|
Replacing a Node in a DOM Tree of an XML Documentby Michelle CopeWe want to hear from you! Please send us your FEEDBACK. The following article may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license.
IntroductionThis article will demonstrate how to replace an existing node in a DOM (Document Object Model) tree of an XML (eXtensible Markup Language) document with the entire DOM tree of another XML document.
ExampleIt is often the case that you have two or more XML files which represent different parts of a complete DOM tree. You may have a DOM tree that has special reference nodes. These special reference nodes indicate where a new DOM subtree may be added. There are two main steps to achieve this functionality. First, finding the node that will be replaced and second, its replacement with a DOM subtree of a second document. In the following code example, the DOM subtree represents the entire document. During the course of this article, 'first DOM tree' will refer to the DOM representation of the document that will have a node replaced, and 'second DOM tree' refers to the DOM representation of the document that will be the replacement node. The node to be replaced is found by searching the DOM tree given certain search criteria. In the code example given, the method findElementNode(String elementName, Node root) performs a depth first search where a successful search will return a reference to the first node with an element name equal to the parameter, elementName . An unsuccessful search will return null.public Node findElementNode(String elementName, Node root){
Node matchingNode = null;
//Check to see if root is the desired element. If so return root.
String nodeName = root.getNodeName();
if((nodeName != null) & (nodeName.equals(elementName)))
return root;
//Check to see if root has any children if not return null
if(!(root.hasChildNodes()))
return null;
//Root has children, so continue searching for them
NodeList childNodes = root.getChildNodes();
int noChildren = childNodes.getLength();
for(int i = 0; i < noChildren; i++){
if(matchingNode == null){
Node child = childNodes.item(i);
matchingNode = findElementNode(elementName,child);
} else break;
}
return matchingNode;
}
Once you have determined which node of the first DOM tree is to be replaced,
you need to create the node that will replace it. The replacement node
will be wrapped in an org.w3c.dom.DocumentFragment
object . A document fragment is a 'lightweight' document object and its
use is preferable for creating or copying large parts of DOM trees. Alternatively,
if the replacement node represents a small subtree then an org.w3c.dom.Node
object could equally be used. A document fragment from the second DOM tree
is created to hold the replacement node. This document fragment is initially
empty until a subtree of the second DOM tree is added. Document Fragments
are treated as DOM tree nodes, and hence can be manipulated using the
tree manipulation methods of org.w3c.dom.Node.
In the code example given, org.w3c.dom.Node.appendChild(Node
node)is invoked on the document fragment to add the subtree as a
child of the document fragment.
The next and most important step is to import the document fragment
into the first DOM tree. Each node has only one owner: the document object
in which the node was created. In order for a node from one DOM tree to
be placed into a second DOM tree, the node must be 'imported' to the second
DOM tree, otherwise an ownership clash will result and an org.w3c.dom.DOMException
will be thrown. Importing nodes will resolve any ownership clashes between
nodes. The
org.w3c.dom.Document.importNode(Node node, boolean deep) method
'imports a node' by creating a node under the ownership of
the first document, and then copying the value of the parameter,
node ,into the newly created node. The second parameter, deep,
is a boolean flag. If deep
equals true then any subtree of the node is copied, otherwise only the
node itself is copied. org.w3c.dom.ImportNode(Node
node, boolean deep) is applied to the first DOM tree and the node
returned is the replacing node. The final step is to replace the
node in the first DOM tree with the replacing node. This is done
by retrieving the parent of the node to be replaced and using org.w3c.dom.Node.replace(Node
replacementNode, Node replacedNode). The following method performs
the preceding steps:
| |||||||||||||||||||||||||||||||||||||||