java - Parsing XML, skip certain tags -
java - Parsing XML, skip certain tags -
i next mkyong's great tutorial on how parse xml in java, have situation need read tags within area. e.g.
<?xml version="1.0"?> <company> <staff id="1001"> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> <changes> <staff id="2001"> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <old_salary>175000</salary> <new_salary>200000</salary> </staff> </changes> </company>
in tutorial uses nodelist nlist = doc.getelementsbytagname("staff");
retrieves of staff, including 1 under <changes>
. how can disregard <staff>
tags under <changes>
?
thanks
using dom parser not best need. using xpath queries should better. (api)
for exemple :
documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document doc = db.parse(new file("test.xml")); xpath xpath = xpathfactory.newinstance().newxpath(); string look = "/company/staff"; //read nodelist using xpath nodelist nodelist = (nodelist) xpath.compile(expression).evaluate(doc, xpathconstants.nodeset);
java xml
Comments
Post a Comment