java - getChildNodes() on org.w3c.dom.Node returns NodeLits of only nulls -
i'm trying parse configuration file, when call getchildnodes() on node excludes, returned nodelist contains 7 null values , nothing else.
my code:
public class minnull { private static final list<string> excludes = new arraylist<>(); public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception { file cfgfile = new file("ver.cfg"); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document doc = db.parse(cfgfile); element rootelement = doc.getdocumentelement(); nodelist cfg = rootelement.getchildnodes(); parseconfig(cfg); } private static void parseconfig(nodelist cfg) { (int = 0; < cfg.getlength(); i++) { node node = cfg.item(i); string name = node.getnodename(); switch (name) { case "excludes": nodelist exc = node.getchildnodes(); for(int j = 0; j < exc.getlength();j++){ node ex = exc.item(i); system.out.println(ex); if(ex != null && ex.getnodename().equals("file")){ excludes.add(ex.gettextcontent()); } } break; } } } }
my xml file (named ver.cfg) :
<?xml version="1.0" encoding="utf-8"?> <root> <server>localhost</server> <port>6645</port> <project>myapp</project> <mainfile>myapp.jar</mainfile> <version>v1</version> <excludes> <file>launcher.jar</file> <file>launch.bat</file> <file>ver.cfg</file> </excludes> </root>
output:
null null null null null null null
everything else working correctly.
you use wrong index variable in
node ex = exc.item(i);
you should have used j
here.
at point i = 5
, , child node 5 not exists, getting null
indicating that node not exists.
Comments
Post a Comment