Skip to main content

Effective XML processing with DOM and XPath in Java

Analysis of many projects yields advice and suggested code


Listing 4. Adding a node

  public static Node appendNode(Node startNode, String name, String xql)
    throws Exception
  {
    Node targetNode = XPathAPI.selectSingleNode( startNode,xql );
    Document doc = startNode.getOwnerDocument();

    Element newElement = doc.createElement( name );
    targetNode.appendChild( (Node) newElement );
    return ( (Node) newElement );
  }

Return to article