Modifying DOM Document Nodes using XPath

Discussion in 'Java' started by O.B., Feb 1, 2006.

  1. O.B.

    O.B. Guest

    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.B., Feb 1, 2006
    #1
    1. Advertising

  2. O.B.

    O.B. Guest

    Re: Modifying DOM Document Nodes using XPath (resolved)

    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/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;
    }
     
    O.B., Feb 1, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Rilling
    Replies:
    1
    Views:
    2,912
    Martin Honnen
    Mar 26, 2005
  2. Replies:
    1
    Views:
    593
    Soren Kuula
    Aug 24, 2006
  3. eggie5
    Replies:
    1
    Views:
    541
    Joseph Kesselman
    Oct 2, 2007
  4. Alan
    Replies:
    6
    Views:
    1,642
  5. SMH
    Replies:
    2
    Views:
    261
Loading...

Share This Page