I am currently working with a XML string (all xml data stored in String xml) that is made of multiple XML files in this format:
<?xml version...>
<File xml:space="preserve">
<Subfile keyword="Store" tag="0">
<Value number="1">Amazon</Value>
</Subfile>
<Subfile keyword="Owner" tag="1">
<Value number="1">Alice Murphy</Value>
</Subfile>
<Subfile keyword="Date" tag="2">
<Value number="1">20161114</Value>
</Subfile>
</File>
<?xml version...>
<File xml:space="preserve">
<Subfile keyword="Store" tag="0">
<Value number="1">Walmart</Value>
</Subfile>
<Subfile keyword="Owner" tag="1">
<Value number="1">Eliza Calvin</Value>
</Subfile>
<Subfile keyword="Date" tag="2">
<Value number="1">20161130</Value>
</Subfile>
</File>
...
I want to retrieve all of the values of "Owner" from xml, but my code apparently only works when there is one xml file. Below is my working code when there is only one xml file within the xml string:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/File/Subfile[@keyword='Owner']/Value";
String owner = xpath.compile(expression).evaluate(document);
How do I modify my code to make sure that even in situations where the xml string has more than one xml files, I can still retrieve all the "Owner" values and store them in something like String owner[]?
Thanks a lot for your help!
<globaltag>and to the end like</globaltag><?xmland its ending tag