{"id":85,"date":"2018-07-26T07:34:03","date_gmt":"2018-07-26T07:34:03","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=85"},"modified":"2018-08-02T06:09:12","modified_gmt":"2018-08-02T06:09:12","slug":"xml-dom-and-java","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/xml-dom-and-java\/","title":{"rendered":"XML DOM and JAVA"},"content":{"raw":"<div>\r\n\r\n<strong>\u00a0 \u00a0XML DOM Parsers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">XML parsers accesses and manipulates an XML DOM tree. Java provides W3C DOM specification as a separate package called org.w3c.dom. In this package, Java provides interfaces and objects together with methods and properties according to the DOM specification. These specification is implemented in java which is used to navigate and manipulate the DOM tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>DOM Parsers<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Greeting.xml<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?xml version=\"1.0\" ?&gt;\r\n\r\n&lt;greeting&gt; Hello World! &lt;\/greeting&gt;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0Element Node<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0Text node<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-86 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32.png\" alt=\"\" width=\"639\" height=\"219\" \/><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\">Figure 12.1 shows that how the xml file 'Greeting.xml' could be parsed into Element node and Text node. The node &lt;greeting&gt; is identified as Element node and the text 'Hello World' is identified as Text node.<\/p>\r\n&nbsp;\r\n\r\n<strong>Creating Document<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to navigate and manipulate an XML file, we need to access the DOM tree for an XML document.The steps for creating document in order to parse the given sample XML document is given below.<\/p>\r\n&nbsp;\r\n\r\nStep 1: First XML related packages has to be imported.\r\n\r\nThe Packages to be imported are,\r\n\r\nimport javax. xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 2: Create an instance of the parser. A parser is created using DocumentBuilder.<\/p>\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 3: To parse an XML document and to create a Document object, the following overloaded methods are used,<\/p>\r\nDocument parse (InputStream in)\r\n\r\nDocument parse (InputStream in, String base)\r\n\r\nDocument parse (String uri)\r\n\r\nDocument parse (File xmlFile)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The following overloaded parse method is used to parse and create a Document object from XML files. Document doc = parser.parse(\u201cgreeting.xml\u201d);<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 Once the Document object is created for the given XML file and then the root element can be extracted. Then the required operations can be performed in the XML document by calling the corresponding functions.<\/span><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong>Navigating DOM Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A node in the DOM may be referenced as, start from the root node and use structural relationships to reach other nodes. To navigate the DOM tree, we use the following methods.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">One method is that we can use the <em>getElementById()<\/em> method of the Document object to access a particular node. The other methods is that using the <em>getElementsByTagName()<\/em> method on the Document object to access all element nodes with a common tag name specified.<\/p>\r\n&nbsp;\r\n\r\n<strong>Using root node<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">W3C defined a property <em>documentElement()<\/em> on the Document object that refers to the root element of the XML document.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The method <em>getDocumentElement()<\/em> is used on the Document object to retrieve the root node which is an Element node.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Element root = doc.getDocumentElement();<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-87 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33.png\" alt=\"\" width=\"550\" height=\"151\" \/><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The following snippet code is for retrieving the name of the root node and printing it. The method <em>getNodeName() <\/em>is used for retrieving the name of a node.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String name = root.getNodeName(); System.out.println (name);<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\/\/prints \u201cgreeting\u201d<\/span><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The following snippet code is used for retrieving the text content of the element. The method <\/span><em style=\"text-align: initial;font-size: 1em\">getFirstChild() <\/em><span style=\"text-align: initial;font-size: 1em\">is to retrieve the first child of the root node that retrieves the text node from the XML file. Once the node is retrieved, we can print the value of the node using the method <\/span><em style=\"text-align: initial;font-size: 1em\">getNodevalue()<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/p>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0Text node= (Text) root.getFirstChild();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0String txt = node.getNodeValue();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 System.out.println(txt);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\"> \/\/ prints \u201cHello World\u201d<\/span>\r\n<div>\r\n\r\n<strong>\u00a0 <\/strong>\r\n\r\n<strong>\u00a0Getting all child nodes<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The XML document \u201cquestions.xml\u201d is defined below. The XML document describes about question papers each with question id and text content of the question.<\/p>\r\n&nbsp;\r\n\r\n&lt;?xml version = \"1.0\"?&gt;\r\n\r\n&lt;question-paper&gt;\r\n\r\n&lt;question id = \"q1\"&gt;\r\n\r\nWhat is DOM?\r\n\r\n&lt;\/question&gt;\r\n\r\n&lt;question id = \"q2\"&gt;\r\n\r\nWhat are Leaves?\r\n\r\n&lt;\/question&gt;\r\n\r\n&lt;\/question-paper&gt;\r\n\r\n&nbsp;\r\n\r\nFigure 12.3 shows how this XML file can be viewed as DOM tree. The root node is &lt;question-paper&gt;. The &lt;question&gt; node becomes the child of the root node. The &lt;question&gt; node has an attribute 'id' and child node which is the text node.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong><img class=\"size-full wp-image-88 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34.png\" alt=\"\" width=\"421\" height=\"235\" \/><\/strong><\/p>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Example 1 - Getting all child nodes<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This program code explains about how to get all the child nodes and print the value of all the nodes. First we create a <em>Document<\/em> object using the parse method on the <em>DocumentBuilder<\/em> object. Use the method <em>getDocumentElement()<\/em> method to retrieve the root node. Once the root node is retrieved, we can navigate the tree. To retrieve all the child nodes use the method <em>getChildNodes()<\/em> on the root which returns a <em>NodeList<\/em>. NodeList is an array, so we can use the method <em>getLength()<\/em> to find the number of children of the root. Use a for loop to read every child with the method <em>item()<\/em> on the NodeList object. Once we retrieve very node, use the method <em>getNodeType()<\/em> to retrieve the type of the node. If the type of the node is an ELEMENT_NODE then find the first child of the Element node using the method <em>getFirstChild() <\/em>and print the value of the node which is the Text node using the method<em> getNodeValue()<\/em>.<\/p>\r\n&nbsp;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\nclass GetQuestion\r\n\r\n{\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\ntry\r\n\r\n{\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();\r\n\r\nDocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc =parser.parse(\"questions.xml\");\r\n\r\nElement root=doc.getDocumentElement();\r\n\r\n&nbsp;\r\n\r\nNodeList children=root.getChildNodes(); <strong>\/\/get all children of root<\/strong> System.out.println(children.getLength());\r\n\r\n<strong>\/\/Getting all child nodes<\/strong>\r\n\r\nfor(int i=0;i&lt;children.getLength();i++)\r\n\r\n{\r\n\r\nNode node=children.item(i);\r\n\r\nif(node.getNodeType()==Node.ELEMENT_NODE)\r\n\r\nSystem.out.println(node.getFirstChild().getNodeValue());\r\n\r\n}\r\n\r\n}catch(Exception e)\r\n\r\n{\r\n\r\ne.printStackTrace();\r\n\r\n}}\r\n\r\n}\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-89 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"377\" height=\"176\" \/><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 12.4 shows the output of getting all child nodes from the root. The root node &lt;question-paper&gt; is retrieved and the children of the root is the list of &lt;question&gt; nodes. Every &lt;question&gt; node is retrieved whose child is the text node and whose value is printed as output of this program code.<\/p>\r\n&nbsp;\r\n\r\n<strong>Using getElementsByTagName<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A list of elements having a tag name can be obtained using the method <em>getElementsByTagName()<\/em>.In the given XML file the &lt;question&gt; elements may be obtained by specifying the name of the element passed as parameter to the method <em>getElementsByTagName().<\/em><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">NodeList children=doc.getElementsByTagName(\u201cquestion\u201d); This method can also be invoked on the root element.<\/p>\r\n&nbsp;\r\n\r\nNodeList children =root.getElementsByTagName(\u201cquestion\u201d);\r\n\r\n&nbsp;\r\n\r\nThen the list of Question elements can be obtained using the similar procedure as mentioned in the previous example.\r\n\r\n&nbsp;\r\n\r\nfor(int i=0; i&lt;children.getLength(); i++)\r\n\r\n{\r\n\r\nNode node=children.item(i);\r\n\r\nSystem.out.println(node.getFirstChild().getNodeValue());\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Using getElementsById<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe method <em>getElementsById()<\/em> on the Document object is used to get an element with a specified id.\r\n\r\n&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;!DOCTYPE question-paper [\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!ELEMENT question-paper (question+)&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!ELEMENT question (#PCDATA)&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!ATTLIST question id ID #REQUIRED&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">]&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;question-paper&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;question id=\"q1\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">What is DOM?<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/question&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;question id=\"q2\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">What are leaves?<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/question&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/question-paper&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">To retrieve the element by 'ID' we use the method <em>getElementById(\u201cq1\u201d)<\/em> passing the value of the id. Element e = doc.getElementById(\u201cq1\u201d);<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Getting attributes of an element<\/strong>\r\n\r\n&nbsp;\r\n\r\nAll Attributes of a context element are obtained using,\r\n\r\n&nbsp;\r\n\r\nNamedNodeMap getAttributes()\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe value of a specific attribute can be obtained using the following method on the element node. String getAttribute(String attributeName);\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe method is defined on the Element object. Typecast the Node object to the Element object first and then apply the getAttribute() method\r\n\r\n&nbsp;\r\n\r\nString value=((Element) Node).getAttribute(\u201cno\u201d);\r\n\r\n&nbsp;\r\n\r\nIf the node is really an Element type node, can be invoked directly without typecasting. String value=e.getAttribute(\u201cno\u201d);\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example 2 - Getting Elements by ID.<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">As explained earlier, first we create a <\/span><em style=\"text-align: initial;font-size: 1em\">Document<\/em><span style=\"text-align: initial;font-size: 1em\"> object using the parse method on the <\/span><em style=\"text-align: initial;font-size: 1em\">DocumentBuilder<\/em><span style=\"text-align: initial;font-size: 1em\"> object. Use the method <\/span><em style=\"text-align: initial;font-size: 1em\">getElementById(\"q1\")<\/em><span style=\"text-align: initial;font-size: 1em\"> by passing the ID as 'q1' to retrieve the Element node with ID 'q1'. Once the Element node is retrieved, use the method <\/span><em style=\"text-align: initial;font-size: 1em\">getAttribute()<\/em><span style=\"text-align: initial;font-size: 1em\"> to pass the attribute name of the node and retrieve the attribute's value. Then print the value which prints the Question no. as 'q1'. Then get the first child of this Element node which is the Text node and print the node value as ' What is DOM?'.<\/span><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\npublic class GetElementById {\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\ntry\r\n\r\n{\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em>\r\n\r\nDocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc=parser.parse(\"question1.xml\");\r\n\r\nElement e=doc.getElementById(\"q1\");\r\n\r\nString value=e.getAttribute( \"id\" );\r\n\r\nSystem.out.print( value + \".\" ); \/\/ prints the question no\r\n\r\nSystem.out.println( e.getFirstChild().getNodeValue() );\r\n\r\n}catch( Exception e )\r\n\r\n{\r\n\r\ne.printStackTrace();\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe following Figure.12.5 shows the output of the above example code which retrieves the Element using the method getElementById().<strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-95 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"376\" height=\"192\" \/><\/p>\r\n<p style=\"text-align: left\"><strong style=\"font-size: 1em\">Example 3 - Getting Elements by TagName<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">As explained earlier, first we create a <em>Document<\/em> object using the parse method on the <em>DocumentBuilder<\/em> object. We can retrieve the list of elements having a tag name 'question' using the method <em>getElementsByTagName()<\/em>. Then we can use a for loop to navigate each element and print the attribute value by passing the attribute name to the <em>getAttribute()<\/em> method. We then print the Node value of each element.<\/p>\r\n&nbsp;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\npublic class GetAttribute {\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\ntry{\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em>\r\n\r\nDocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc=parser.parse(\"questions.xml\");\r\n\r\nElement root=doc.getDocumentElement();\r\n\r\n&nbsp;\r\n\r\nNodeList children =root.getElementsByTagName(\"question\"); for(int i=0;i&lt;children.getLength();i++)\r\n\r\n{\r\n\r\nNode node=children.item(i);\r\n\r\nString value=((Element)node).getAttribute(\"id\");\r\n\r\nSystem.<em>out.print(value+\".\");<\/em>\r\n\r\nSystem.<em>out.println(node.getFirstChild().getNodeValue());<\/em>\r\n\r\n}\r\n\r\n}catch(Exception e){ e.printStackTrace(); } } }\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-96 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"473\" height=\"270\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">Viewing DOM<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A DOM tree may be transformed back to an XML document which can be displayed on the screen and stored in a file. This helps us to visualize and verify the DOM tree after adding or deleting nodes.This can be done by creating Transformer object and a DOMSource object. The task of the Transformer object is to transform the specified XML document to the specified stream.We can use the method <em>transform(source, result)<\/em> on the Transformer object to transform the DOM source to output stream.An example to display the result after transformation to the standard output is given below,<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 4: Viewing DOM<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport javax.xml.transform.*;\r\n\r\nimport javax.xml.transform.dom.DOMSource;\r\n\r\nimport javax.xml.transform.stream.StreamResult;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\nimport javax.xml.transform.*;\r\n\r\nimport javax.xml.transform.dom.DOMSource;\r\n\r\nimport javax.xml.transform.stream.StreamResult;\r\n\r\npublic class view {\r\n\r\n{\r\n\r\ntry {\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em> DocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc=parser.parse(\"questions.xml\");\r\n\r\nElement root=doc.getDocumentElement();\r\n\r\nTransformerFactory tFactory = TransformerFactory.newInstance();\r\n\r\nTransformer transformer = tFactory.newTransformer();\r\n\r\nDOMSource source = new DOMSource(root); StreamResult result=new StreamResult(System.out); transformer.transform(source, result); }\r\n\r\ncatch(Exception e)\r\n\r\n{\r\n\r\ne.printStackTrace();\r\n\r\n}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n}\r\n\r\nThe Figure 12.7 shows the output of viewing the XML file.\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-90 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"537\" height=\"213\" \/><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Manipulating DOM Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">With the XML DOM, Java can access and change all the elements of an XML document. A DOM tree can be manipulated by adding or deleting node. we can also set attributes to the element node.<\/p>\r\n&nbsp;\r\n\r\n<strong>Creating a node<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Text type node is created using the createTextNode() method on the Document node. Text createTextNode(String text)<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Once created, to attach to an Element node, We need to create an Element node.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The method signature is<\/p>\r\n<p style=\"text-align: justify\">Element createElement(String elementname)<\/p>\r\nElement e = doc.createElement(\u201cquestion\u201d);\r\n\r\n&nbsp;\r\n\r\n<strong>Setting an attribute<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The attribute of an element is set using the setAttribute() method of the element node. The method signature is<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">setAttribute(String attributename, String attributevalue); e.setAttribute(\u201cid\u201d, \u201cq3\u201d);<\/p>\r\n&nbsp;\r\n\r\n<strong>Adding a node<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe Text node can now be attached to the Element node e using the appendChild() method.\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">.appendChild(txt);<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The element node e is attached to the root element<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">root.appendChild(e);<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong>Example 5: Creating Nodes and Adding Nodes<\/strong>\r\n\r\n&nbsp;\r\n\r\nAs explained earlier, first we create a <em>Document<\/em> object using the parse method on the <em>Document Builder\u00a0<\/em>object. Then we create a Transformer object and DOMSource object to view the XML file before adding elements. We then create a Text node using the method, <em>createTextNode( \"What is DTD?\" )<\/em> by passing the text value. Then we can create the create Element node using the method <em>createElement(<\/em><em>\"question\" )<\/em> by passing the name of the element 'question'. Now we can append this created Text node to the Element node using the method <em>appendChild( txt )<\/em>. Then set the attribute for the Element node with attribute name as 'id' and value as q3' using the method <em>setAttribute( \"id\", \"q3\" )<\/em>. Now append the created Element node to the root node of the XML DOM using <em>root.appendChild(e)<\/em>.\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\nimport javax.xml.transform.*;\r\n\r\nimport javax.xml.transform.dom.DOMSource;\r\n\r\nimport javax.xml.transform.stream.StreamResult;\r\n\r\npublic class AppendQuestion {\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\ntry{\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em>\r\n\r\nDocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc=parser.parse( \"questions.xml\" );\r\n\r\nElement root=doc.getDocumentElement();\r\n\r\nTransformerFactory tFactory=TransformerFactory.<em>newInstance();<\/em>\r\n\r\nTransformer transformer=tFactory.newTransformer();\r\n\r\nDOMSource source=new DOMSource( root ); StreamResult result=new StreamResult( System.<em>out<\/em> ); System.<em>out.println( \"Before addition\" );<\/em> transformer.transform(source , result);\r\n\r\nText txt=doc.createTextNode( \"What is DTD?\" );\r\n\r\nElement e=doc.createElement( \"question\" );\r\n\r\ne.appendChild( txt );\r\n\r\ne.setAttribute( \"id\", \"q3\" );\r\n\r\nroot.appendChild(e);\r\n\r\nSystem.<em>out.println( \"\\n After Addition\" );<\/em>\r\n\r\ntransformer.transform(source,result);\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">FileOutputStream fout=new FileOutputStream( new File( \"out.xml\" ) ); StreamResult result1=new StreamResult( fout ); transformer.transform( source, result1 ); }catch( Exception e )<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">e.printStackTrace();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\nThe output of the above example code is shown in Figure 12.8.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-91 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"641\" height=\"356\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Creating Clone<\/strong>\r\n\r\n&nbsp;\r\n\r\nA copy of a node can be obtained using the method,\r\n\r\nNode aCopy=questions.item(0).cloneNode(true);\r\n\r\nThen set the attribute to the copy node, ((Element)aCopy).setAttribute(\"id\",\"q3\");\r\n\r\nReplace the Text value of the Text node using,\r\n\r\n((Text)aCopy.getFirstChild()).replaceWholeText(\"What is XML?\");\r\n\r\nAppend this clone node to the root of the XML DOM.\r\n\r\nroot.appendChild(aCopy);\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong>Example 6: Creating Clone Node and Appending<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport javax.xml.parsers.*;\r\n\r\nimport org.w3c.dom.*;\r\n\r\nimport javax.xml.transform.*;\r\n\r\nimport javax.xml.transform.dom.DOMSource;\r\n\r\nimport javax.xml.transform.stream.StreamResult;\r\n\r\npublic class CopyQuestion {\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\ntry\r\n\r\n{\r\n\r\nDocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em>\r\n\r\nDocumentBuilder parser=factory.newDocumentBuilder();\r\n\r\nDocument doc=parser.parse(\"questions.xml\");\r\n\r\nElement root=doc.getDocumentElement();\r\n\r\nTransformerFactory tFactory=TransformerFactory.<em>newInstance();<\/em>\r\n\r\nTransformer transformer=tFactory.newTransformer();\r\n\r\n&nbsp;\r\n\r\nDOMSource source=new DOMSource(root); StreamResult result=new StreamResult(System.<em>out);<\/em> System.<em>out.println(\"Before addition\");<\/em> <em>transformer.transform(source, result);<\/em>\r\n\r\n&nbsp;\r\n\r\nNodeList questions=doc.getElementsByTagName(\"question\"); Node aCopy=questions.item(0).cloneNode(true); ((Element)aCopy).setAttribute(\"id\",\"q3\"); ((Text)aCopy.getFirstChild()).replaceWholeText(\"What is XML?\"); root.appendChild(aCopy);\r\n\r\nSystem.<em>out.println(\"\\n After Addition\");<\/em>\r\n\r\ntransformer.transform(source,result);\r\n\r\n}catch(Exception e)\r\n\r\n{\r\n\r\ne.printStackTrace();\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe output of the above example code is shown in Figure 12.9.\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-92 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"633\" height=\"348\" \/><\/strong>\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module discusses about how to navigate and manipulate XML DOM using Java. The module also explores with examples how to view DOM, manipulate DOM by creating nodes, cloning nodes and adding to the DOM tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li><em>www.w3schools.com\/<strong>xml<\/strong><\/em><\/li>\r\n \t<li><em>www.cis.upenn.edu\/~matuszek\/cit597-2003\/Lectures\/08-<strong>dtd<\/strong>.<strong>ppt<\/strong><\/em><\/li>\r\n \t<li><em>Uttam Kumar Roy<strong>, \"<\/strong> Web Technologies\", Oxford University Press, 2010.<\/em><\/li>\r\n<\/ul>\r\n&nbsp;","rendered":"<div>\n<p><strong>\u00a0 \u00a0XML DOM Parsers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">XML parsers accesses and manipulates an XML DOM tree. Java provides W3C DOM specification as a separate package called org.w3c.dom. In this package, Java provides interfaces and objects together with methods and properties according to the DOM specification. These specification is implemented in java which is used to navigate and manipulate the DOM tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>DOM Parsers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Greeting.xml<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; ?&gt;<\/p>\n<p>&lt;greeting&gt; Hello World! &lt;\/greeting&gt;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0Element Node<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0Text node<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-86 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32.png\" alt=\"\" width=\"639\" height=\"219\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32.png 639w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32-300x103.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32-225x77.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-32-350x120.png 350w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\">Figure 12.1 shows that how the xml file &#8216;Greeting.xml&#8217; could be parsed into Element node and Text node. The node &lt;greeting&gt; is identified as Element node and the text &#8216;Hello World&#8217; is identified as Text node.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating Document<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to navigate and manipulate an XML file, we need to access the DOM tree for an XML document.The steps for creating document in order to parse the given sample XML document is given below.<\/p>\n<p>&nbsp;<\/p>\n<p>Step 1: First XML related packages has to be imported.<\/p>\n<p>The Packages to be imported are,<\/p>\n<p>import javax. xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 2: Create an instance of the parser. A parser is created using DocumentBuilder.<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 3: To parse an XML document and to create a Document object, the following overloaded methods are used,<\/p>\n<p>Document parse (InputStream in)<\/p>\n<p>Document parse (InputStream in, String base)<\/p>\n<p>Document parse (String uri)<\/p>\n<p>Document parse (File xmlFile)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The following overloaded parse method is used to parse and create a Document object from XML files. Document doc = parser.parse(\u201cgreeting.xml\u201d);<\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 Once the Document object is created for the given XML file and then the root element can be extracted. Then the required operations can be performed in the XML document by calling the corresponding functions.<\/span><\/p>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Navigating DOM Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A node in the DOM may be referenced as, start from the root node and use structural relationships to reach other nodes. To navigate the DOM tree, we use the following methods.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One method is that we can use the <em>getElementById()<\/em> method of the Document object to access a particular node. The other methods is that using the <em>getElementsByTagName()<\/em> method on the Document object to access all element nodes with a common tag name specified.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using root node<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">W3C defined a property <em>documentElement()<\/em> on the Document object that refers to the root element of the XML document.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The method <em>getDocumentElement()<\/em> is used on the Document object to retrieve the root node which is an Element node.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Element root = doc.getDocumentElement();<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-87 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33.png\" alt=\"\" width=\"550\" height=\"151\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33.png 550w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33-300x82.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33-225x62.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-33-350x96.png 350w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The following snippet code is for retrieving the name of the root node and printing it. The method <em>getNodeName() <\/em>is used for retrieving the name of a node.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">String name = root.getNodeName(); System.out.println (name);<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\/\/prints \u201cgreeting\u201d<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The following snippet code is used for retrieving the text content of the element. The method <\/span><em style=\"text-align: initial;font-size: 1em\">getFirstChild() <\/em><span style=\"text-align: initial;font-size: 1em\">is to retrieve the first child of the root node that retrieves the text node from the XML file. Once the node is retrieved, we can print the value of the node using the method <\/span><em style=\"text-align: initial;font-size: 1em\">getNodevalue()<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0Text node= (Text) root.getFirstChild();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0String txt = node.getNodeValue();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 System.out.println(txt);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\"> \/\/ prints \u201cHello World\u201d<\/span><\/p>\n<div>\n<p><strong>\u00a0 <\/strong><\/p>\n<p><strong>\u00a0Getting all child nodes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The XML document \u201cquestions.xml\u201d is defined below. The XML document describes about question papers each with question id and text content of the question.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?xml version = &#8220;1.0&#8221;?&gt;<\/p>\n<p>&lt;question-paper&gt;<\/p>\n<p>&lt;question id = &#8220;q1&#8221;&gt;<\/p>\n<p>What is DOM?<\/p>\n<p>&lt;\/question&gt;<\/p>\n<p>&lt;question id = &#8220;q2&#8221;&gt;<\/p>\n<p>What are Leaves?<\/p>\n<p>&lt;\/question&gt;<\/p>\n<p>&lt;\/question-paper&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Figure 12.3 shows how this XML file can be viewed as DOM tree. The root node is &lt;question-paper&gt;. The &lt;question&gt; node becomes the child of the root node. The &lt;question&gt; node has an attribute &#8216;id&#8217; and child node which is the text node.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-88 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34.png\" alt=\"\" width=\"421\" height=\"235\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34.png 421w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34-300x167.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34-225x126.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-34-350x195.png 350w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/strong><\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Example 1 &#8211; Getting all child nodes<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This program code explains about how to get all the child nodes and print the value of all the nodes. First we create a <em>Document<\/em> object using the parse method on the <em>DocumentBuilder<\/em> object. Use the method <em>getDocumentElement()<\/em> method to retrieve the root node. Once the root node is retrieved, we can navigate the tree. To retrieve all the child nodes use the method <em>getChildNodes()<\/em> on the root which returns a <em>NodeList<\/em>. NodeList is an array, so we can use the method <em>getLength()<\/em> to find the number of children of the root. Use a for loop to read every child with the method <em>item()<\/em> on the NodeList object. Once we retrieve very node, use the method <em>getNodeType()<\/em> to retrieve the type of the node. If the type of the node is an ELEMENT_NODE then find the first child of the Element node using the method <em>getFirstChild() <\/em>and print the value of the node which is the Text node using the method<em> getNodeValue()<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>class GetQuestion<\/p>\n<p>{<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();<\/p>\n<p>DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc =parser.parse(&#8220;questions.xml&#8221;);<\/p>\n<p>Element root=doc.getDocumentElement();<\/p>\n<p>&nbsp;<\/p>\n<p>NodeList children=root.getChildNodes(); <strong>\/\/get all children of root<\/strong> System.out.println(children.getLength());<\/p>\n<p><strong>\/\/Getting all child nodes<\/strong><\/p>\n<p>for(int i=0;i&lt;children.getLength();i++)<\/p>\n<p>{<\/p>\n<p>Node node=children.item(i);<\/p>\n<p>if(node.getNodeType()==Node.ELEMENT_NODE)<\/p>\n<p>System.out.println(node.getFirstChild().getNodeValue());<\/p>\n<p>}<\/p>\n<p>}catch(Exception e)<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}}<\/p>\n<p>}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-89 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"377\" height=\"176\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35.png 377w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35-300x140.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35-225x105.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-35-350x163.png 350w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 12.4 shows the output of getting all child nodes from the root. The root node &lt;question-paper&gt; is retrieved and the children of the root is the list of &lt;question&gt; nodes. Every &lt;question&gt; node is retrieved whose child is the text node and whose value is printed as output of this program code.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using getElementsByTagName<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A list of elements having a tag name can be obtained using the method <em>getElementsByTagName()<\/em>.In the given XML file the &lt;question&gt; elements may be obtained by specifying the name of the element passed as parameter to the method <em>getElementsByTagName().<\/em><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">NodeList children=doc.getElementsByTagName(\u201cquestion\u201d); This method can also be invoked on the root element.<\/p>\n<p>&nbsp;<\/p>\n<p>NodeList children =root.getElementsByTagName(\u201cquestion\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>Then the list of Question elements can be obtained using the similar procedure as mentioned in the previous example.<\/p>\n<p>&nbsp;<\/p>\n<p>for(int i=0; i&lt;children.getLength(); i++)<\/p>\n<p>{<\/p>\n<p>Node node=children.item(i);<\/p>\n<p>System.out.println(node.getFirstChild().getNodeValue());<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using getElementsById<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The method <em>getElementsById()<\/em> on the Document object is used to get an element with a specified id.<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<\/p>\n<p>&lt;!DOCTYPE question-paper [<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!ELEMENT question-paper (question+)&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!ELEMENT question (#PCDATA)&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!ATTLIST question id ID #REQUIRED&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">]&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;question-paper&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;question id=&#8221;q1&#8243;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">What is DOM?<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/question&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;question id=&#8221;q2&#8243;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">What are leaves?<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/question&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/question-paper&gt;<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">To retrieve the element by &#8216;ID&#8217; we use the method <em>getElementById(\u201cq1\u201d)<\/em> passing the value of the id. Element e = doc.getElementById(\u201cq1\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Getting attributes of an element<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>All Attributes of a context element are obtained using,<\/p>\n<p>&nbsp;<\/p>\n<p>NamedNodeMap getAttributes()<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The value of a specific attribute can be obtained using the following method on the element node. String getAttribute(String attributeName);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The method is defined on the Element object. Typecast the Node object to the Element object first and then apply the getAttribute() method<\/p>\n<p>&nbsp;<\/p>\n<p>String value=((Element) Node).getAttribute(\u201cno\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>If the node is really an Element type node, can be invoked directly without typecasting. String value=e.getAttribute(\u201cno\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 2 &#8211; Getting Elements by ID.<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">As explained earlier, first we create a <\/span><em style=\"text-align: initial;font-size: 1em\">Document<\/em><span style=\"text-align: initial;font-size: 1em\"> object using the parse method on the <\/span><em style=\"text-align: initial;font-size: 1em\">DocumentBuilder<\/em><span style=\"text-align: initial;font-size: 1em\"> object. Use the method <\/span><em style=\"text-align: initial;font-size: 1em\">getElementById(&#8220;q1&#8221;)<\/em><span style=\"text-align: initial;font-size: 1em\"> by passing the ID as &#8216;q1&#8217; to retrieve the Element node with ID &#8216;q1&#8217;. Once the Element node is retrieved, use the method <\/span><em style=\"text-align: initial;font-size: 1em\">getAttribute()<\/em><span style=\"text-align: initial;font-size: 1em\"> to pass the attribute name of the node and retrieve the attribute&#8217;s value. Then print the value which prints the Question no. as &#8216;q1&#8217;. Then get the first child of this Element node which is the Text node and print the node value as &#8216; What is DOM?&#8217;.<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>public class GetElementById {<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em><\/p>\n<p>DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc=parser.parse(&#8220;question1.xml&#8221;);<\/p>\n<p>Element e=doc.getElementById(&#8220;q1&#8221;);<\/p>\n<p>String value=e.getAttribute( &#8220;id&#8221; );<\/p>\n<p>System.out.print( value + &#8220;.&#8221; ); \/\/ prints the question no<\/p>\n<p>System.out.println( e.getFirstChild().getNodeValue() );<\/p>\n<p>}catch( Exception e )<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The following Figure.12.5 shows the output of the above example code which retrieves the Element using the method getElementById().<strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-95 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"376\" height=\"192\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39.png 376w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39-300x153.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39-225x115.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-39-350x179.png 350w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/p>\n<p style=\"text-align: left\"><strong style=\"font-size: 1em\">Example 3 &#8211; Getting Elements by TagName<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As explained earlier, first we create a <em>Document<\/em> object using the parse method on the <em>DocumentBuilder<\/em> object. We can retrieve the list of elements having a tag name &#8216;question&#8217; using the method <em>getElementsByTagName()<\/em>. Then we can use a for loop to navigate each element and print the attribute value by passing the attribute name to the <em>getAttribute()<\/em> method. We then print the Node value of each element.<\/p>\n<p>&nbsp;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>public class GetAttribute {<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>try{<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em><\/p>\n<p>DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc=parser.parse(&#8220;questions.xml&#8221;);<\/p>\n<p>Element root=doc.getDocumentElement();<\/p>\n<p>&nbsp;<\/p>\n<p>NodeList children =root.getElementsByTagName(&#8220;question&#8221;); for(int i=0;i&lt;children.getLength();i++)<\/p>\n<p>{<\/p>\n<p>Node node=children.item(i);<\/p>\n<p>String value=((Element)node).getAttribute(&#8220;id&#8221;);<\/p>\n<p>System.<em>out.print(value+&#8221;.&#8221;);<\/em><\/p>\n<p>System.<em>out.println(node.getFirstChild().getNodeValue());<\/em><\/p>\n<p>}<\/p>\n<p>}catch(Exception e){ e.printStackTrace(); } } }<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-96 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"473\" height=\"270\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40.png 473w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40-300x171.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-40-350x200.png 350w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">Viewing DOM<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A DOM tree may be transformed back to an XML document which can be displayed on the screen and stored in a file. This helps us to visualize and verify the DOM tree after adding or deleting nodes.This can be done by creating Transformer object and a DOMSource object. The task of the Transformer object is to transform the specified XML document to the specified stream.We can use the method <em>transform(source, result)<\/em> on the Transformer object to transform the DOM source to output stream.An example to display the result after transformation to the standard output is given below,<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 4: Viewing DOM<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import javax.xml.transform.*;<\/p>\n<p>import javax.xml.transform.dom.DOMSource;<\/p>\n<p>import javax.xml.transform.stream.StreamResult;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>import javax.xml.transform.*;<\/p>\n<p>import javax.xml.transform.dom.DOMSource;<\/p>\n<p>import javax.xml.transform.stream.StreamResult;<\/p>\n<p>public class view {<\/p>\n<p>{<\/p>\n<p>try {<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em> DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc=parser.parse(&#8220;questions.xml&#8221;);<\/p>\n<p>Element root=doc.getDocumentElement();<\/p>\n<p>TransformerFactory tFactory = TransformerFactory.newInstance();<\/p>\n<p>Transformer transformer = tFactory.newTransformer();<\/p>\n<p>DOMSource source = new DOMSource(root); StreamResult result=new StreamResult(System.out); transformer.transform(source, result); }<\/p>\n<p>catch(Exception e)<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p>\n<p>}<\/p>\n<p>The Figure 12.7 shows the output of viewing the XML file.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-90 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"537\" height=\"213\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36.png 537w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36-225x89.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-36-350x139.png 350w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Manipulating DOM Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">With the XML DOM, Java can access and change all the elements of an XML document. A DOM tree can be manipulated by adding or deleting node. we can also set attributes to the element node.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating a node<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Text type node is created using the createTextNode() method on the Document node. Text createTextNode(String text)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Once created, to attach to an Element node, We need to create an Element node.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The method signature is<\/p>\n<p style=\"text-align: justify\">Element createElement(String elementname)<\/p>\n<p>Element e = doc.createElement(\u201cquestion\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Setting an attribute<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The attribute of an element is set using the setAttribute() method of the element node. The method signature is<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">setAttribute(String attributename, String attributevalue); e.setAttribute(\u201cid\u201d, \u201cq3\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Adding a node<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The Text node can now be attached to the Element node e using the appendChild() method.<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">.appendChild(txt);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The element node e is attached to the root element<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">root.appendChild(e);<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong>Example 5: Creating Nodes and Adding Nodes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>As explained earlier, first we create a <em>Document<\/em> object using the parse method on the <em>Document Builder\u00a0<\/em>object. Then we create a Transformer object and DOMSource object to view the XML file before adding elements. We then create a Text node using the method, <em>createTextNode( &#8220;What is DTD?&#8221; )<\/em> by passing the text value. Then we can create the create Element node using the method <em>createElement(<\/em><em>&#8220;question&#8221; )<\/em> by passing the name of the element &#8216;question&#8217;. Now we can append this created Text node to the Element node using the method <em>appendChild( txt )<\/em>. Then set the attribute for the Element node with attribute name as &#8216;id&#8217; and value as q3&#8242; using the method <em>setAttribute( &#8220;id&#8221;, &#8220;q3&#8221; )<\/em>. Now append the created Element node to the root node of the XML DOM using <em>root.appendChild(e)<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>import javax.xml.transform.*;<\/p>\n<p>import javax.xml.transform.dom.DOMSource;<\/p>\n<p>import javax.xml.transform.stream.StreamResult;<\/p>\n<p>public class AppendQuestion {<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>try{<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em><\/p>\n<p>DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc=parser.parse( &#8220;questions.xml&#8221; );<\/p>\n<p>Element root=doc.getDocumentElement();<\/p>\n<p>TransformerFactory tFactory=TransformerFactory.<em>newInstance();<\/em><\/p>\n<p>Transformer transformer=tFactory.newTransformer();<\/p>\n<p>DOMSource source=new DOMSource( root ); StreamResult result=new StreamResult( System.<em>out<\/em> ); System.<em>out.println( &#8220;Before addition&#8221; );<\/em> transformer.transform(source , result);<\/p>\n<p>Text txt=doc.createTextNode( &#8220;What is DTD?&#8221; );<\/p>\n<p>Element e=doc.createElement( &#8220;question&#8221; );<\/p>\n<p>e.appendChild( txt );<\/p>\n<p>e.setAttribute( &#8220;id&#8221;, &#8220;q3&#8221; );<\/p>\n<p>root.appendChild(e);<\/p>\n<p>System.<em>out.println( &#8220;\\n After Addition&#8221; );<\/em><\/p>\n<p>transformer.transform(source,result);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">FileOutputStream fout=new FileOutputStream( new File( &#8220;out.xml&#8221; ) ); StreamResult result1=new StreamResult( fout ); transformer.transform( source, result1 ); }catch( Exception e )<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">e.printStackTrace();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p>The output of the above example code is shown in Figure 12.8.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-91 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"641\" height=\"356\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37.png 641w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37-300x167.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-37-350x194.png 350w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating Clone<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A copy of a node can be obtained using the method,<\/p>\n<p>Node aCopy=questions.item(0).cloneNode(true);<\/p>\n<p>Then set the attribute to the copy node, ((Element)aCopy).setAttribute(&#8220;id&#8221;,&#8221;q3&#8243;);<\/p>\n<p>Replace the Text value of the Text node using,<\/p>\n<p>((Text)aCopy.getFirstChild()).replaceWholeText(&#8220;What is XML?&#8221;);<\/p>\n<p>Append this clone node to the root of the XML DOM.<\/p>\n<p>root.appendChild(aCopy);<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Example 6: Creating Clone Node and Appending<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import javax.xml.parsers.*;<\/p>\n<p>import org.w3c.dom.*;<\/p>\n<p>import javax.xml.transform.*;<\/p>\n<p>import javax.xml.transform.dom.DOMSource;<\/p>\n<p>import javax.xml.transform.stream.StreamResult;<\/p>\n<p>public class CopyQuestion {<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>DocumentBuilderFactory factory=DocumentBuilderFactory.<em>newInstance();<\/em><\/p>\n<p>DocumentBuilder parser=factory.newDocumentBuilder();<\/p>\n<p>Document doc=parser.parse(&#8220;questions.xml&#8221;);<\/p>\n<p>Element root=doc.getDocumentElement();<\/p>\n<p>TransformerFactory tFactory=TransformerFactory.<em>newInstance();<\/em><\/p>\n<p>Transformer transformer=tFactory.newTransformer();<\/p>\n<p>&nbsp;<\/p>\n<p>DOMSource source=new DOMSource(root); StreamResult result=new StreamResult(System.<em>out);<\/em> System.<em>out.println(&#8220;Before addition&#8221;);<\/em> <em>transformer.transform(source, result);<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>NodeList questions=doc.getElementsByTagName(&#8220;question&#8221;); Node aCopy=questions.item(0).cloneNode(true); ((Element)aCopy).setAttribute(&#8220;id&#8221;,&#8221;q3&#8243;); ((Text)aCopy.getFirstChild()).replaceWholeText(&#8220;What is XML?&#8221;); root.appendChild(aCopy);<\/p>\n<p>System.<em>out.println(&#8220;\\n After Addition&#8221;);<\/em><\/p>\n<p>transformer.transform(source,result);<\/p>\n<p>}catch(Exception e)<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above example code is shown in Figure 12.9.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-92 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"633\" height=\"348\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38.png 633w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-38-350x192.png 350w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/strong><\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module discusses about how to navigate and manipulate XML DOM using Java. The module also explores with examples how to view DOM, manipulate DOM by creating nodes, cloning nodes and adding to the DOM tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li><em>www.w3schools.com\/<strong>xml<\/strong><\/em><\/li>\n<li><em>www.cis.upenn.edu\/~matuszek\/cit597-2003\/Lectures\/08-<strong>dtd<\/strong>.<strong>ppt<\/strong><\/em><\/li>\n<li><em>Uttam Kumar Roy<strong>, &#8220;<\/strong> Web Technologies&#8221;, Oxford University Press, 2010.<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":12,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-85","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":12,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/85\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/85\/revisions\/526"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/85\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=85"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=85"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}