Java XML Null Node -
this question has answer here:
xml file:
<?xml version="1.0" encoding="utf-8"?> <personnel> <employee type="permanent"> <name>ali</name> <id>3674</id> <age>34</age> </employee> <employee type="contract"> <name>hasan</name> <id>3675</id> <age>25</age> </employee> <employee type="permanent"> <name>ahmet</name> <id>3676</id> <age>28</age> </employee> </personnel>
xml.java:
public class xml{ documentbuilderfactory dfk; document doc; public void xmlparse(string xmlfile) throws parserconfigurationexception, saxexception, ioexception { dfk= documentbuilderfactory.newinstance(); doc= dfk.newdocumentbuilder().parse(new file(xmlfile)); xmlparse(); } public void xmlparse(){ doc.getdocumentelement().normalize(); element element = doc.getdocumentelement(); nodelist nodelist = element.getelementsbytagname("personnel"); (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); } } }
hello all; how can name, id, age etc. try more times not work show null.. sory bad english.. ( xml file > correctly < )
you try elements , children. started:
public void xmlparse(string xmlfile) throws parserconfigurationexception, saxexception,ioexception { documentbuilderfactory docbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder docbuilder = docbuilderfactory.newdocumentbuilder(); document document = docbuilder.parse(new file(xmlfile)); xmlparse(document.getdocumentelement()); } private void xmlparse(element node) { nodelist nodelist = node.getchildnodes(); (int = 0; < nodelist.getlength(); i++) { node currentnode = nodelist.item(i); string value = currentnode.getnodename(); system.out.print(value + " "); // prints employee nodelist nodes = currentnode.getchildnodes(); (int j = 0; j < nodes.getlength(); j++) { node cnode = nodes.item(j); system.out.print(cnode.getnodename() + " ");//prints:name, id, age } system.out.println(); } }
output is:
#text employee #text name #text id #text age #text #text employee #text name #text id #text age #text #text employee #text name #text id #text age #text #text
Comments
Post a Comment