Below is the basic example for Java XML DOM Parser.
Step-1: Initially, the packages that are related to XML are imported.
[java]
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
[/java]
inside the above code, org.w3c.dom characterizes the dom programming interfaces for xml records focused by w3c and javax.xml.parsers characterizes documentbuilderfactory tastefulness and documentbuilder polish, which gives back a thing that actualizes w3c record interface. this package likewise characterizes parserconfigurationexception style for reporting botches.
Step-2: Then create the above mentioned DocumentBuilderFactory class and DocumentBuilder class.
[java]
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
[/java]
Step-3: A XML document should be created from file or a stream. In the below code,
parse()
is used to parse document.
[java]
StringBuilder xmlStringBuilder = new StringBuilder();
xmlStringBuilder.append("<?xml version="1.0"?> <class> </class>");
ByteArrayInputStream input = new ByteArrayInputStream(
xmlStringBuilder.toString().getBytes("UTF-8"));
Document doc = builder.parse(input);
[/java]
Step-4: Here, the root element have to be extracted.
[java]Element root = document.getDocumentElement();[/java]
Step-5: The attributes and sub-elements are tested.
[java]
//returns specific attribute
getAttribute("attributeName");
//returns a Map (table) of names/values
getAttributes();
[/java]
[java]
//returns a list of subelements of specified name
getElementsByTagName("subelementName");
//returns a list of all child nodes
getChildNodes();
[/java]
If kept all the above steps at once, below is the input XML document that has to be parsed.
[xml]<?xml version="1.0"?>
<class>
<student rollno="393">
<firstname>John</firstname>
<lastname>Mike</lastname>
<nickname>Jom</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Rafeal</firstname>
<lastname>Nadal</lastname>
<nickname>Rafa</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Samuel</firstname>
<lastname>Johnson</lastname>
<nickname>Sam</nickname>
<marks>90</marks>
</student>
</class>[/xml]
DomParserDemo.java:
[java]
package com.splessons.xml;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class DomParserDemo {
public static void main(String[] args){
try {
File inputFile = new File("input.txt");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("student");
System.out.println("----------------------------");
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) {
Element eElement = (Element) nNode;
System.out.println("Student roll no : "
+ eElement.getAttribute("rollno"));
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();
}
}
}
[/java]
The javax.xml.Parsers.DocumentBuilderFactory class characterizes a processing plant API that empowers applications to acquire a parser that produces DOM protest trees from XML records.
Output:
[java]
Root element :class
----------------------------
Current Element :student
Student roll no : 393
First Name : John
Last Name : Mike
Nick Name : Jom
Marks : 85
Current Element :student
Student roll no : 493
First Name : Rafeal
Last Name : Nadal
Nick Name : Rafa
Marks : 95
Current Element :student
Student roll no : 593
First Name : Samuel
Last Name : Johnson
Nick Name : Sam
Marks : 90
[/java]