Modifying DOM Document Nodes using XPath

O

O.B.

Given a DOM Document object that has been parsed from an XML file, I am
using org.apache.xpath.XPathAPI to selectSingleNode for modification.
Unfortunately, the returned node is static. Is there a way to modify
the returned node such that the changes are propagated back into the
Document object?
 
O

O.B.

O.B. said:
Given a DOM Document object that has been parsed from an XML file, I am
using org.apache.xpath.XPathAPI to selectSingleNode for modification.
Unfortunately, the returned node is static. Is there a way to modify
the returned node such that the changes are propagated back into the
Document object?

This works:

http://www-128.ibm.com/developerworks/library/x-domjava/listing3.html

public static Node setValue(Node startNode, String value, String xql)
throws Exception
{
Node targetNode = XPathAPI.selectSingleNode( startNode,xql );

NodeList children = targetNode.getChildNodes();
int index = 0;
int length = children.getLength();

// Remove all of the current contents
for(index = 0; index < length; index++) {
targetNode.removeChild( children.item( index ) );
}

// Add in the new value
Document doc = startNode.getOwnerDocument();
targetNode.appendChild( doc.createTextNode(value) );

return targetNode;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top