parsing an XML with JAVA -
i trying parse few specific parts of xml doc. looking @ pulling data out of analysis section, need warnings, errors, passes , need go each of sections () , result , result level , text example in "error" need level of error , text "error".
<document> <configuration> </configuration> <data> </data> <analysis warnings="5" errors="3" information="0" passed="false"> <files> </files> <results> <form> <section number="0"> <result level="error">error</result> <result level="error">error</result> <result level="error">error</result> <result level="warning">warning</result> <result level="warning">warning</result> </section> <section number="1"> <result level="warning">warning</result> </section> <section number="2"> <result level="warning">warning</result> <result level="warning">warning</result> </section> </form> </results> </analysis> </document>
i have following code:
public void processxmlfrompath(string path) throws exception { documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); document document = builder.parse(path); nodelist nodelist = document.getdocumentelement().getchildnodes(); (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); if (node instanceof element) { system.out.println(node.getattributes().tostring()); nodelist childnodes = node.getchildnodes(); (int j = 0; j < childnodes.getlength(); j++) { node cnode = childnodes.item(j); if (cnode instanceof element) { system.out.println(cnode.getnodename().tostring()); if(cnode.getnodename().tostring() == "analysis") { string content = cnode.getlastchild().gettextcontent().trim(); system.out.println(content); //i thought print children under analysis section screen mistaken. make point. } } } } } }
the thing i'm getting print console is:
configuration data analysis
any appreciated!
a couple of issues code:
cnode.getnodename().tostring() == "analysis"
, string comparison.equals
analysis
direct descendant ofdocument
(per xml piece have here), has checked on. code checks @level 3
instead of2
- you need drill further down analysis
results
,form
,text
nodes.
edit: based on comments, efficient way traverse without multiple loops woild recursion, below:
public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception { inputstream path = new fileinputstream("sample.xml"); documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); document document = builder.parse(path); traverse(document.getdocumentelement()); } public static void traverse(node node) { nodelist list = node.getchildnodes(); (int = 0; < list.getlength(); i++) { node currentnode = list.item(i); traverse(currentnode); } if (node.getnodename().equals("result")) { system.out.println("this -> " + node.gettextcontent()); } }
this gives result as:
this -> error -> error -> error -> warning -> warning -> warning -> warning -> warning
Comments
Post a Comment