Java parse xml file when node inner text is html -


right i'm using saxparser own handler, can parse node values except 1 has type="html"

my characters function this:

public void characters(char ch[], int start, int length) throws saxexception {         if(content){         string tmp = new string(ch, start, length);         system.out.println("content : " + tmp);         content = false;         } 

and particular node has following format, output give me bunch of \n , nothing else.

   <content type="html">      &lt;img alt="" src="http://cdn2.sbnation.com/entry_photo_images/8767829/stranger-bad-robot-screencap_large.png" /&gt;        &lt;p&gt;bad robot, production company founded geek culture hitmaker j.j. abrams (&lt;i&gt;lost&lt;/i&gt;, &lt;i&gt;fringe&lt;/i&gt;, &lt;i&gt;star trek: darkness&lt;/i&gt;, &lt;i&gt;alias&lt;/i&gt;,&amp;nbsp;etc.), has released a&amp;nbsp;&lt;a href="http://youtu.be/fwaazcaqxdo" target="_blank"&gt;mysterious new trailer&lt;/a&gt; titled "stranger." creepy , inscrutable video spot, posted official bad robot twitter account afternoon, features starry sky; long-haired, rope-bound man wandering along desolate monochromatic shore line; , garden variety, horrifying stitched-mouth person coming focus. "men erased , reborn," intones narrator sounds little leonard nimoy.&lt;/p&gt;      &lt;p&gt;&lt;/p&gt;        </content> 

you might wrongfully assuming characters callback occurs once in between startelement , endelement callbacks. called multiple times.

since use content boolean member determine whether print stuff or not , set same member false inside characters callback, condition bound fulfilled once, until reset content (it not clear that).

here's example works xml fine (assumes non-mixed content , java programming language):

import java.io.ioexception; import java.io.stringreader; import javax.xml.parsers.parserconfigurationexception; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import org.xml.sax.attributes; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler;  public class testsaxparser {      public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception {         string xml =              "<content type=\"html\">\n" +             "\n" +             "    &lt;img alt=\"\" src=\"http://cdn2.sbnation.com/entry_photo_images/8767829/stranger-bad-robot-screencap_large.png\" /&gt;\n" +             "\n" +             "\n" +             "     &lt;p&gt;bad robot, production company founded geek culture hitmaker j.j. abrams (&lt;i&gt;lost&lt;/i&gt;, &lt;i&gt;fringe&lt;/i&gt;, &lt;i&gt;star trek: darkness&lt;/i&gt;, &lt;i&gt;alias&lt;/i&gt;,&amp;nbsp;etc.), has released a&amp;nbsp;&lt;a href=\"http://youtu.be/fwaazcaqxdo\" target=\"_blank\"&gt;mysterious new trailer&lt;/a&gt; titled \"stranger.\" creepy , inscrutable video spot, posted official bad robot twitter account afternoon, features starry sky; long-haired, rope-bound man wandering along desolate monochromatic shore line; , garden variety, horrifying stitched-mouth person coming focus. \"men erased , reborn,\" intones narrator sounds little leonard nimoy.&lt;/p&gt;\n" +             "     &lt;p&gt;&lt;/p&gt;\n" +             "\n" +             "\n" +             "\n" +             "    </content>";          mysaxhandler handler = new mysaxhandler();         saxparserfactory factory = saxparserfactory.newinstance();         saxparser parser = factory.newsaxparser();                 inputsource source = new inputsource(new stringreader(xml));         parser.parse(source, handler);     }      private static class mysaxhandler extends defaulthandler {         private stringbuilder content = new stringbuilder();          @override         public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception {             content.setlength(0);         }          @override         public void characters(char[] ch, int start, int length) throws saxexception {             content.append(ch, start, length);         }          @override         public void endelement(string uri, string localname, string qname) throws saxexception {             system.out.println(content.tostring());         }      }     } 

output:

    <img alt="" src="http://cdn2.sbnation.com/entry_photo_images/8767829/stranger-bad-robot-screencap_large.png" />        <p>bad robot, production company founded geek culture hitmaker j.j. abrams (<i>lost</i>, <i>fringe</i>, <i>star trek: darkness</i>, <i>alias</i>,&nbsp;etc.), has released a&nbsp;<a href="http://youtu.be/fwaazcaqxdo" target="_blank">mysterious new trailer</a> titled "stranger." creepy , inscrutable video spot, posted official bad robot twitter account afternoon, features starry sky; long-haired, rope-bound man wandering along desolate monochromatic shore line; , garden variety, horrifying stitched-mouth person coming focus. "men erased , reborn," intones narrator sounds little leonard nimoy.</p>      <p></p> 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -