Posted in

To Read XML File Using DOM Parser In Java

Java Program To Parser An XML File Using DOM Parser


DOM Parser

DOM Stands For Document Object Model. It characterizes an interface to access process and style, structure and substance of XML reports
The DOM Parser gives functions of capacities to use the XML documents to inspect the substance and structure of the report. It also checks the proper format of the XML file.

Example Of DOM Parser


AIM:

To write a java program to parse an XML file using DOM parser.

PROCEDURE:

STEP 1: Create a XML document.

STEP 2: Create a java program with the DOM API.

STEP 3: Parse the XML document using the java program.

STEP 4: Display the contents of the XML document with appropriate levels.

Source Code Of This Program

1.XML File [DomParser.xml]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0"?>
<Shopping_Details>
    <person id="1001">
        <firstname>Dinesh</firstname>
        <lastname>Karthick</lastname>
        <orders>5 Orders</orders>
        <phone>9688946524</phone>
        <address>567,anna nagar,chennai-06</address>
    </person>
    <person id="1002">
        <firstname>Suresh</firstname>
        <lastname>kumar</lastname>
        <orders>10 Orders</orders>
        <phone>9600783578</phone>
        <address>67,Punga Nagar,Pudukkottai-622005</address>
    </person>
    <person id="1003">
        <firstname>Arul</firstname>
        <lastname>Prakash</lastname>
        <orders>8 Orders</orders>
        <phone>8954902346</phone>
        <address>45,vadapalani,chennai-07</address>
    </person>
</Shopping_Details>

2.Java File [Domparser.java]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
import java.io.File;

public class Domparser{

  public static void main(String argv[]) {

    try {

    File fXmlFile = new File("Domparser.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
            
   doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            
    NodeList nList = doc.getElementsByTagName("person");
            
    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("person id : " + eElement.getAttribute("id"));
            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("Orders : " + eElement.getElementsByTagName("orders").item(0).getTextContent());
            System.out.println("Phone : " + eElement.getElementsByTagName("phone").item(0).getTextContent());
            System.out.println("Address : " + eElement.getElementsByTagName("address").item(0).getTextContent());

        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
     System.out.println("----------------------------------");
  }
}

NOTE: ALL THE TWO FILES ARE  IN THE SAME FOLDER THAT’S ONLY YOU CAN RUN THE CORRECT OUTPUT OF THE RESULT.

 

I am a Software Engineer with a strong interest in writing technology-related articles, managing two websites. I am dedicated to learning new technologies and producing engaging content. Additionally, I possess expertise in digital marketing and SEO strategies.

Leave a Reply

Your email address will not be published. Required fields are marked *