java - How to get element's value from XML using SAX parser in startElement? -
is possible content of element xml file in startelement
function override function of sax handler?
below specification.
1) xml file
<employees> <employee id="111"> <firstname>rakesh</firstname> <lastname>mishra</lastname> <location>bangalore</location> </employee> <employee id="112"> <firstname>john</firstname> <lastname>davis</lastname> <location>chennai</location> </employee> <employee id="113"> <firstname>rajesh</firstname> <lastname>sharma</lastname> <location>pune</location> </employee> </employees>
2) startelement
function
@override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { .......code in here.......... }
3) expected result
element name : employee attribute name : id attribute value: 111 firstname : rakesh lastname : mishra location : bangalore element name : employee attribute name : id attribute value: 112 firstname : john lastname : davis location : chennai element name : employee attribute name : id attribute value: 113 firstname : rajesh lastname : sharma location : pune
you can element's name in startelement
, endelement
. can attributes in startelement
. values should in characters
.
here very basic example on how value of element using contenthandler
:
public class yourhandler extends defaulthandler { boolean infirstnameelement = false; public class startelement(....) { if(qname.equals("firstname") { infirstnameelement = true; } } public class endelement(....) { if(qname.equals("firstname") { infirstnameelement = false; } } public class characters(....) { if(infirstnameelement) { // characters in <firstname> element } } }
if have simple example, setting boolean flags each tag ok. if have more complex scenario, might prefer store flags in map using element names keys, or create 1 or more employee
classes mapped xml, instantiate them every time <employee>
found in startelement
, populate properties, , add collection in endelement
.
here complete contenthandler
example works example file. hope helps started:
public class simplehandler extends defaulthandler { class employee { public string firstname; public string lastname; public string location; public map<string, string> attributes = new hashmap<>(); } boolean isfirstname, islastname, islocation; employee currentemployee; list<employee> employees = new arraylist<>(); @override public void startelement(string uri, string localname, string qname, attributes atts) throws saxexception { if(qname.equals("employee")) { currentemployee = new employee(); for(int = 0; < atts.getlength(); i++) { currentemployee.attributes.put(atts.getqname(i),atts.getvalue(i)); } } if(qname.equals("firstname")) { isfirstname = true; } if(qname.equals("lastname")) { islastname = true; } if(qname.equals("location")) { islocation = true; } } @override public void endelement(string uri, string localname, string qname) throws saxexception { if(qname.equals("employee")) { employees.add(currentemployee); currentemployee = null; } if(qname.equals("firstname")) { isfirstname = false; } if(qname.equals("lastname")) { islastname = false; } if(qname.equals("location")) { islocation = false; } } @override public void characters(char[] ch, int start, int length) throws saxexception { if (isfirstname) { currentemployee.firstname = new string(ch, start, length); } if (islastname) { currentemployee.lastname = new string(ch, start, length); } if (islocation) { currentemployee.location = new string(ch, start, length); } } @override public void enddocument() throws saxexception { for(employee e: employees) { system.out.println("employee id: " + e.attributes.get("id")); system.out.println(" first name: " + e.firstname); system.out.println(" last name: " + e.lastname); system.out.println(" location: " + e.location); } } }
Comments
Post a Comment