The document describes how to work with XML documents using DOM and Java. It shows how to parse an XML file into a DOM document, retrieve and modify elements and attributes, add new elements, write the modified DOM document back to an XML file, and output the DOM document to the console. Code examples demonstrate parsing, traversing, modifying, and writing an XML document that contains student data and car information.
4. DomParserDemo.java
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DomParserDemo
{
public static void main(String[] args){
try {
inputFile = new File("input.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
The normalize() method merges adjacent text() nodes and removes empty ones in the
whole document.
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
//returns a list of subelements of specified name
NodeList nList = doc.getElementsByTagName("student");
System.out.println("----------------------------");
5. for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
System.out.println("nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE)// check for element node
{
Element eElement = (Element) nNode;
System.out.println("Student roll no : " + eElement.getAttribute("rollno"));
6. System.out.println(First Name : " + eElement.getElementsByTagName(firstname") .item(0) .getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname") .item(0) .getTextContent());
System.out.println(Nick Name : " + eElement.getElementsByTagName("nickname") .item(0) .getTextContent());
System.out.println(Marks: " + eElement.getElementsByTagName("marks") .item(0) .getTextContent());
}
}
}
catch (Exception e)
{
e.printStackTrace(); //useful tool for diagnosing an Exception
}
}
}
7. Output will be
Root element :class
----------------------------
Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkarMarks : 85
Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinniMarks : 95
Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singh
Nick Name : jazz
Marks : 90
11. // write the content into xml file
TransformerFactory transformerFactory= TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:cars.xml"));
transformer.transform(source, result); // Output to console for testing
StreamResult consoleResult =new StreamResult(System.out);
transformer.transform(source, consoleResult);
}
catch (Exception e)
{
e.printStackTrace();
} }}
12. OUTPUT will be
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cars>
<supercars company="Ferrari">
<carname type="formula one">Ferrari 101</carname>
<carname type="sports">Ferrari 202</carname>
</supercars>
</cars>
(you will have cars.xml created in C:>