XPath gets text from multiple nodes

I need to create a StringArray with nametext from:

<xs:element name="xyz" type="xs:string/>

<xs:element name="bla" type="xs:string/>

...

How can I request "xyz", "bla" and many others?

Probably the worst code you've ever seen, but anyway:

 NodeList result1 = (NodeList) xPath.evaluate("//@name", example, XPathConstants.NODESET); for(int i=0; i<result1.getLength();i++) { System.out.println("read 1:" +result1.item(i)); } //console output is: //read 1:name="xyz" //read 1:name="bla" ArrayList<String> liste; liste = new ArrayList<String>(result1.getLength()); for (int i=0; i<result1.getLength();i++){ String read=xPath.evaluate("//@name", example); liste.add(read); System.out.println("read 2: "+read); } System.out.println("complete list: " +liste); //console output is: //read 2:name="xyz" //read 2:name="xyz" //complete list: [xyz, xyz] 

Thanks for the help, made it work as follows:

(just in case .. if anyone ever seeks a solution here)

 NodeList result = (NodeList) xPath.evaluate("//@name", example, XPathConstants.NODESET); liste = new ArrayList<String>(result.getLength()); for(int i=0; i<result.getLength();i++){ liste.add(result.item(i).getNodeValue()); } return(liste); 
+4
source share
1 answer

It looks like you are successfully retrieving a list of results, but then you look at them and reevaluate XPath at each iteration. It seems that the values ​​are printed correctly the first time result1 is run, so why don't you just replace this:

 String read=xPath.evaluate("//@name", example); 

with this:

 String read = result1.item(i).toString(); 
0
source

All Articles