java - How to retrieve prefixed child elements using JDom -



java - How to retrieve prefixed child elements using JDom -

i have next xml snippet trying retrieve first element using jdom getting nullpointer exception.please help me out if 1 knows.

<db1:customer xmlns:db1="http://www.project1.com/db1"> <db1:customerid>22</db1:customerid> <db1:customername>prasad44</db1:customername> <db1:address>chennai</db1:address> <db1:email>pkk@gmail.com</db1:email> <db1:lastupdate>2014-08-01t00:00:00+05:30</db1:lastupdate> <db1:namedetail>bsm_resteter</db1:namedetail> <db1:phonebiz>9916347942</db1:phonebiz> <db1:phonehome>9916347942</db1:phonehome> <db1:phonemobile>944990031</db1:phonemobile> <db1:rating>22</db1:rating> </db1:customer>

here doing,

saxbuilder builder = new saxbuilder(); file xmlfile = new file("commonfiles/file.xml"); document doc = (document) builder.build(xmlfile); element rootnode = doc.getrootelement(); element customerid = rootnode.getchild("sure:customerid"); system.out.println("customerid ======"+customerid);

the print statement displays null.

when dealing xml containing namespaces, need utilize namespace instance that's appropriate document. in case, have:

<db1:customer xmlns:db1="http://www.project1.com/db1">

the namespace here http://www.project1.com/db1 , prefix db1.

in jdom can creaate reference namespace with:

namespace db1 = namespace.getnamespace("db1", "http://www.project1.com/db1");

now, when retrieve content in document, use:

element customerid = rootnode.getchild("customerid", db1);

note content using namespace object, not prefix element (there no "db1:" prefix "customerid"

java xml jdom-2

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -