xml - java xpath list concatination -
i using java xpathfactory values simple xml file:
<obama> <coolnessid>0</coolnessid> <cars>0</cars> <cars>1</cars> <cars>2</cars> </obama> with xpression //obama/coolnessid | //obama/cars result is:
0 0 1 2 from result, cannot distinguish between coolnessid , car id. need like:
coolnessid: 0 carid: 0 carid: 1 carid: 2 with concat('c_id: ', //obama/coolnessid,' car_id: ',//obama/cars) close solution, concat cannot used list of values. unfortunately, cannot use string-join, because seems not known in xpath library. , cannot manipulate given xml.
what other tricks can use list of values alias?
if select elements rather text content you'll have context:
public static void main(string[] args) throws exception { string xml = "<obama>" + " <coolnessid>0</coolnessid>" + " <cars>0</cars>" + " <cars>1</cars>" + " <cars>2</cars>" + "</obama>"; documentbuilderfactory factory = documentbuilderfactory.newinstance(); factory.setnamespaceaware(true); document doc = factory.newdocumentbuilder().parse(new bytearrayinputstream(xml.getbytes(standardcharsets.utf_8))); xpath xpath = xpathfactory.newinstance().newxpath(); xpathexpression expr = xpath.compile("//obama/cars | //obama/coolnessid"); nodelist result = (nodelist) expr.evaluate(doc, xpathconstants.nodeset); (int = 0; < result.getlength(); i++) { element item = (element) result.item(i); system.out.println(item.gettagname() + ": " + item.gettextcontent()); } }
Comments
Post a Comment