java - Using the SAX characters method to parse PCDATA from an XML element -


i'm using sax api parse in xml document, struggling store element pcdata each location within xml.

the oracle docs sax api show characters() used parse in pcdata element, i'm not sure on how supposed called.

in current implementation, boolean flags used signal when element within xml document has been encountered. flags being triggered in startelement() should when element encountered.

i set breakpoint on boolean variable description in charaters() boolean isn't set true until startelement() called, meaning pcdata never parsed.

my question how can call characters() after boolean values set in startelement() ?

this startelement() called after charaters():

public void startelement(string namespaceuri, string localname, string qname, attributes atts) throws saxexception {         if (qname.equals("location")){             location = true;              system.out.println("found location...");             try {                 //read in values attributes of element <location>                 int locationid = integer.parseint(atts.getvalue("id"));                 string locationname = atts.getvalue("name");                   //generate new instance of location on-the-fly using reflection. statement class.forname("gmit.location").newinstance(); invokes                  //java class loader , calls null (default) constructor of location.                 location loc = (location) class.forname("gmit.location").newinstance();                 loc.setid(locationid); //now configure location object id, name, description etc...                 loc.setname(locationname);                 loc.setdescription(locationdescription);               } catch (exception e) {                 e.printstacktrace();             }          }else if (qname.equals("description")){             description = true;             //need invoke charaters method here after description              //flag set true             system.out.println("found description. should tie last location encountered...");      } 

the charaters() called program starts, needs called after boolean flags set in above method:

public void characters(char[] ch,int start, int length) throws saxexception{         if (location){          }else if (description){              locationdescription = new string( ch, start, length);              system.out.println("description = " + locationdescription);      } 

sample of 1 of locations within xml file:

<location id="1" name="tiberius">         <description>         in city of tiberius. see long street high buildings , castle.you see exit south.         </description>         <exit title="desert" direction="s"/>     </location> 

how can call characters() after boolean values set in startelement() ?

you can't. whole point of sax parsing parser calls handler, don't call parser.

your characters method called each time character data encountered in document sax parser. handler need decide whether data relevant (is location, description, or can ignored?) , if relevant store data somewhere can retrieved later.

you've shown startelement method using. if haven't done already, want override endelement. need set boolean values location , description false in endelement method, sax handler knows no longer inside location or description element appropriate.

you haven't shown sample xml document. perhaps have this:

 <widgetlist>      <widget>          <name>first widget</name>          <location>over there</location>          <description>this first widget in list</description>      </widget>      <widget>          <name>second widget</name>          <location>very far away</location>          <description>this second widget in list</description>      </widget> </widgetlist> 

if so, may want handle end of widget element well. example, take last location , description handler encountered, put them in widget object , store in list inside handler. @ end of parsing can read list of widgets handler.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -