Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Modifying DOM Document Nodes using XPath

Reply
Thread Tools

Modifying DOM Document Nodes using XPath

 
 
O.B.
Guest
Posts: n/a
 
      02-01-2006
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?
 
Reply With Quote
 
 
 
 
O.B.
Guest
Posts: n/a
 
      02-01-2006
O.B. wrote:
> 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/developerwork.../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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getNextSibling() never ends? DOM XML nodes (org.w3c.dom) Alan Java 6 10-13-2008 05:48 PM
xpath select all nodes under document root eggie5 XML 1 10-02-2007 07:43 PM
Building SVG document Using DOM Interface Inside HTML Document SMH Javascript 2 03-18-2007 08:57 AM
Selecting Nodes Using Subtotal of Child Nodes renfrochris@hotmail.com XML 1 08-24-2006 12:30 AM
How to use XPath to get list of nodes from XML DOM? Peter Rilling Java 1 03-26-2005 02:09 PM



Advertisments