Not able to parse inner elements of XML using DocumentBuilderFactory in Java -


i'm having response xml. i'm trying parse xml object inner details. im using documentbuilderfactory this. parent object not null, when try deepnode list elements, returning null. missing anything

here response xml

responsexml

<datapacket request-id = "1"> <header>         </header> <body>           <consumer_profile2>         <consumer_details2>                          <name>david</name>             <date_of_birth>1949-01-01t00:00:00+03:00</date_of_birth>             <gender>001</gender>         </consumer_details2>       </consumer_profile2></body></datapacket> 

and im parsing in following way

documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(responsexml)); // consumer details.  if(doc.getdocumentelement().getelementsbytagname("consumer_details2") != null) {                           node consumerdetailsnode = doc.getdocumentelement().getelementsbytagname("consumer_details2").item(0); -->this coming null                             dateofbirth = getnameditem(consumerdetailsnode, "date_of_birth");                                            system.out.println("dob:"+dateofbirth);                       } 

getnameditem

private static string getnameditem(node searchresultnode, string param) {     return searchresultnode.getattributes().getnameditem(param) != null ? searchresultnode.getattributes().getnameditem(param).getnodevalue() : ""; } 

any ideas appreciated.

the easiest way search individual elements within xml document xpath. provides search syntax similar file system notation. here solution specific problem of document:

edit: solution adopted support multiple consumer_profile2 elements. need , parse nodelist instread of 1 node

import java.io.*; import javax.xml.parsers.*; import javax.xml.xpath.*; import org.w3c.dom.*; import org.xml.sax.*;  public class xpathdemo {     public static void main(string[] args)     {         try {             documentbuilderfactory factory = documentbuilderfactory.newinstance();             documentbuilder builder = factory.newdocumentbuilder();             document xmldoc = builder.parse(new inputsource(new filereader("c://temp/xx.xml")));              // selects consumer_profile2 elements no matter in document             string cp2_nodes = "//consumer_profile2";             // selects first date_of_birth element somewhere under current element             string dob_nodes = "//date_of_birth[1]";             // selects text child node of current element             string text_node = "/child::text()";              xpath xpath =  xpathfactory.newinstance().newxpath();             nodelist dob_list = (nodelist)xpath.compile(cp2_nodes + dob_nodes + text_node)                     .evaluate(xmldoc, xpathconstants.nodeset);             (int = 0; < dob_list.getlength() ; i++) {                 node dob_node = dob_list.item(i);                 string dob_text = dob_node.getnodevalue();                 system.out.println(dob_text);             }         } catch (exception e) {             e.printstacktrace();         }     } } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -