java - Parse xml without tagname -
i have xml file
<response> <statuscode>0</statuscode> <statusdetail>ok</statusdetail> <accountinfo> <element1>value</element1> <element2>value</element2> <element3>value</element2> <elementn>value</elementn> </accountinfo> </response>
and want parse elements in accountinfo, dont know elements tag names.
now im using , have code tests, in future recieve more elemenets in accountinfo , dont know how many or there names
string name=""; string balance=""; node accountinfo = document.getelementsbytagname("accountinfo").item(0); if (accountinfo.getnodetype() == node.element_node){ element accountinfoelement = (element) accountinfo; name = accountinfoelement.getelementsbytagname("name").item(0).gettextcontent(); balance = accountinfoelement.getelementsbytagname("balance").item(0).gettextcontent(); }
heres 2 ways can it:
node accountinfo = document.getelementsbytagname("accountinfo").item(0); nodelist children = accountinfo.getchildnodes();
or can do
xpath xpath = xpathfactory.newinstance().newxpath(); nodelist children = (nodelist) xpath.evaluate("//accountinfo/*", document.getdocumentelement(), xpathconstants.nodeset);
once have nodelist
can loop through them.
for(int i=0;i<children.getlength();i++) { if(children.item(i).getnodetype() == node.element_node) { element elem = (element)children.item(i); // if document namespace aware use localname string localname = elem.getlocalname(); // tag name returns localname , namespace prefix string tagname= elem.gettagname(); // stuff children } }
Comments
Post a Comment