In this section, you will see how to create a child node in xml document.
In this section, you will see how to create a child node in xml document.In this section we will discuss about how to add a Child Node in Dom document.
The addition of child node is a very logical. To add a child node first we create blank DOM document and then we create the root node using createElement() method . This root node is appended in the dom document using method appendChild().
After this step, we create child Node(element) for appending with the root node and we can set the attribute of the node by using method setAttribute("name","value") and append this child with the root node.
In this last step, we create the instance of TransformerFactory to create the object of the transformer class. This class is used to print the DOM tree (output) on the console.
The full code of the createchild.java:
import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;
class createchild
Element childElement = doc.createElement("spring"); |
Output of the code:
c:\>java createchild <?xml version="1.0"encoding="UTF-8"standalone="no"?> <roseindia> <spring/> </roseindia> |