On the internet I am finding a lot of examples of how to achieve this functionality, however none of them seem to work in my case. I have an string containing XML data, and I know what the XPath of the result should look like. Say for example I have an xml structure looking like this:
<?xml version="1.0"?>
<m:parent xmlns:m="http://www.somescheme">
<doc:header xmlns:doc="http://www.somescheme">
<doc:documentId>137</doc:documentId>
<doc:documentDescription>Some Description</doc:documentDescription>
<doc:task>
<doc:id>49</doc:id>
<doc:name>Some Name</doc:name>
<doc:description>Some Task Description</doc:description>
<doc:outcome/>
<doc:dueDate/>
<doc:priority>50</doc:priority>
</doc:task>
</doc:header>
<m:otherinfo>234324</m:otherInfo>
</m:parent>
Say for example I need to access and update otherInfo the XPath would be 'm:parent/m:otherInfo' which I should be able to read and set. For doc:outcome it would be 'm:parent/doc:header/doc:task/doc:outcome' This is a value I need to set at runtime.
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource("data.xml"));
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate("//m:parent/doc:header/doc:task/doc:outcome", doc,
XPathConstants.NODESET);
The code should be something like above, however I am not getting results,
Can you point out what I am missing ?
Kind Regards Max
No comments:
Post a Comment