1

I have an xml like:

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <result>
    <street>a</street>
    </result>

    <result>
    <street>b</street>
    </result>

    <result>
    <street>c</street>
    </result>
</results>

I want to obtain a list of string containing all the street values.

In my example it is:

[a,b,c]

With XPath I can write anything like:

//street

I tried anything like:

XpathParser.INSTANCE.evaluate(xml, expression);

But I can only obtain a single string, not a list of strings.

Anybody have an idea?

anubis
  • 1,425
  • 5
  • 18
  • 47
  • you get a `NodeList` back if you use standard XPath API in Java ... – UninformedUser Jun 07 '19 at 11:22
  • here is the correct way: https://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java – UninformedUser Jun 07 '19 at 11:23
  • I have tried that, but i have not a XML as Document, but a variable String containing the xml – anubis Jun 07 '19 at 11:31
  • invalid xml. you can only have one root node in any given xml document – spi Jun 07 '19 at 11:31
  • you are right, I updated my xml – anubis Jun 07 '19 at 11:33
  • @anubis you have to learn using Javadoc: https://docs.oracle.com/javase/8/docs/api/index.html?javax/xml/parsers/DocumentBuilder.html - as you can see, there are mutliple methods `parse`, one of them taking an `InputStream` as argument. Conversion of a `String` to `InputStream`? Well, use the search ... – UninformedUser Jun 07 '19 at 11:46
  • Here is the xpath that you can use `string-join(//result/street/text(),",")` – supputuri Jun 07 '19 at 12:53

0 Answers0