How to parse xml file using DOM parser in java

               Parsing is a process which reads the XML document and interprets it. It extracts the data from the xml and make it in usable format for the program.

Let us create an XML file for customers.

“Customers.xml”

<Customers>

  <customer>

    <name>Rajeeva</name>

    <id>c_1</id>

  </customer>

  <customer>

    <name>Jey</name>

    <id>c_2</id>

  </customer>

 <customer>

    <name>Sandha</name>

    <id>c_3</id>

  </customer>

</Customers>

Here, the root node is  ‘Customers’. It has elements ‘customer’ with data ‘name’ and ‘id’.

Let us parse this XML using java program.

To parse the XML file in java, DOM(Document Object Model) is used.

Steps to follow:

  • Import the built in packages javax.xml.parsers.DocumentBuilder, javax.xml.parsers.DocumentBuilderFactory and org.w3c.dom.
  • Create a public class with main() function.
  • Using a try,catch block, add the code.
  • Initially, the xml file is opened by the DOM. For doing this, create an object for  DocumentBuilderFactory as new instance.
  • Using this instance,call the newDocumentBuilder() function and assign it to DocumentBuilder object.
  • This object’s function parse opens the xml file and assign it to the Document object.
  • A NodeList object is created and reads the xml document elements.
  • Using a for loop, the following process is repeated.
  •  Each  and every elements in the node is read separately and printed.

Program:

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

public class ReadXMLFileEg {

    public static void main(String[] args) {

        try {

            DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder1 = factory1.newDocumentBuilder();

            Document doc1 = builder1.parse("Customers.xml");

            NodeList customerList1 = doc1.getElementsByTagName("customer");

            for (int i = 0; i < customerList1.getLength(); i++) {

                org.w3c.dom.Node node = customerList1.item(i);

                if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {

                    Element customer = (Element) node;

                    String name = customer.getElementsByTagName("name").item(0).getTextContent();

                    String id = customer.getElementsByTagName("id").item(0).getTextContent();

                    System.out.println("Customer Name: " + name + ",Customer id: " + id);

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Output:

Compile and run the java program to get the output.

C:\raji\blog>javac ReadXMLFileEg.java

C:\raji\blog>java ReadXMLFileEg

Customer Name: Rajeeva,Customer id: c_1

Customer Name: Jey,Customer id: c_2

Customer Name: Sandha,Customer id: c_3

 Note: both xml and java files should be saved in the same directory.

Hope this code to parse the xml file in java is useful to you. Feel free to ask your queries. Keep Coding!!!

No comments:

Post a Comment