| by Arround The Web | No comments

Java DOM Parser Examples to Parse XML

XML is a markup language that is mainly used to exchange information between two applications such as databases, websites, etc. The XML tags are defined by the user. It is more human-readable than other mark-up languages. Java has many built-in libraries to read the XML documents using Java XML parsers such as DOM parser, SAX parser, etc. The methods of using the Dom parser to read the XML documents using Java are shown in this tutorial.

Prerequisites:

  1. Install the latest version of OpenJDK with JRE.
  2. Install any useful editor to write and execute the Java code. You can check this tutorial to install the Eclipse editor. 

Create an XML File

Create an XML file named “products.xml” with the following script. The root element of this file contains three child elements with the tag name, “Product”. Each “Product” tag has two attributes which are “ID” and “type”. Each “Product” tag has three child elements which are “brand”, “size”, and “price”.

products.xml

<?xml version="1.0"?>

<Products>

 <Product ID="p-001" type="HDD">

   <brand>Samsung</brand>

   <size>500 TB</size>

   <price>70</price>

 </Product>

 <Product ID="p-002" type="Monitor">

   <brand>Dell</brand>

   <size>15"</size>

   <price>100</price>

 </Product>

 <Product ID="p-003" type="Mouse">

   <brand>Samsung</brand>

   <size>Medium</size>

   <price>5</price>

 </Product>

 <Product ID="p-004" type="Keyboard">

   <brand>Logitech</brand>

   <size>Small</size>

   <price>10</price>

 </Product>

</Products>

Java DOM Parser

The Document Object Model (DOM) parser is one of the ways to read the attribute and tag values of the XML document in Java. The new node values can be added, changed, or deleted using this parser. It reads the whole content of the XML document and loads it in a tree structure format. The methods of reading the entire content of a “products.xml” file and searching the particular content based on an attribute value using a DOM parser are shown in the next part of this tutorial.

Read an XML File Using the DOM Parser

Create a Java file with the following code that parses the content of the “products.xml” file and print the content of each “Product” tag in tabular form. The document builder object is created in the script to load the XML document. Next, each node type is checked and the attribute values of “ID” and “type” are read using the getAttribute() method. The getElementsByTagName() method is used to read the content of the “brand”, “size”, and “price” tags.

//Import necessary modules

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 JavaXmlParser {

    public static void main(String[] args) {
       try {
                     
             //Create DocumentBuilder to parse the content of the XML file
             DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder Builder = Factory.newDocumentBuilder();
             
             //Create object from XML file to parse XML document
             Document xmldoc = Builder.parse(new File("products.xml"));
     
             //Create object to read the content of the particular element
             NodeList ele = xmldoc.getElementsByTagName("Product");
             
             //Print the title of the tabular output
             System.out.println("\nID\ttype\tBrand\t\tSize\tPrice\n");
             //Read all attributes and element values of the XML document by using loop
             for (int i = 0; i < ele.getLength(); i++) {
                 //Read the current node
                 Node nd = ele.item(i);
               
                //Check the node type
                if (nd.getNodeType() == Node.ELEMENT_NODE) {
                   Element cur_Element = (Element) nd;
                   System.out.print(cur_Element.getAttribute("ID") + "\t");
                   System.out.print(cur_Element.getAttribute("type") + "\t");
                   System.out.print(cur_Element.getElementsByTagName("brand").item(0).getTextContent() + "\t\t");
                   System.out.print(cur_Element.getElementsByTagName("size").item(0).getTextContent() + "\t");
                   System.out.print("$" + cur_Element.getElementsByTagName("price").item(0).getTextContent() + "\t");
                }
                System.out.println();            
             }
          } catch (Exception e) {
             e.printStackTrace();
       }
         }

}

The following output appears after executing the previous code. The attributes and tag values of the XML document are printed in tabular form. The root tag of the XML document contains three “Product” tags. So, three rows of values are printed in the output:

Search in an XML File Using the DOM Parser

Create a Java file with the following code that searches the content of the particular tag of the “products.xml” file based on an attribute value. The content of the tag is printed if any match is found. An error message is printed if no match is found.

//Import necessary modules

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;

import java.io.*;

public class JavaXmlParser2 {

    public static void main(String[] args) {
        try {
                             
             //Create DocumentBuilder to parse the content of the XML file
             DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder Builder = Factory.newDocumentBuilder();
             
             //Create object from XML file to parse XML document
             Document xmldoc = Builder.parse(new File("products.xml"));
             
             //Create object to read the content of the particular element
             NodeList ele = xmldoc.getElementsByTagName("Product");
             
             //Create object to take ID value that will be searched
             BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
             System.out.print("Enter the product ID: ");
             String search = buf.readLine();
             
             //Set default value
             boolean found = false;
             //Read all attributes and element values of the XML document by using loop
             for (int i = 0; i < ele.getLength(); i++) {
                 //Read the current node
                 Node nd = ele.item(i);
                 
                //Check the node type
                if (nd.getNodeType() == Node.ELEMENT_NODE) {
                   Element cur_Element = (Element) nd;
                   
                   String val = (String) cur_Element.getAttribute("ID");
                   //Check the search ID exists or not
                   if (val.equals(search))
                   {
                       System.out.println("Id: " + cur_Element.getAttribute("ID") + "\t");
                       System.out.println("Type: " + cur_Element.getAttribute("type") + "\t");
                       System.out.println("Brand: " + cur_Element.getElementsByTagName("brand").item(0).getTextContent() + "\t\t");
                       System.out.println("Price: $" + cur_Element.getElementsByTagName("price").item(0).getTextContent() + "\t");
                       found = true;
                       break;
                   }
                   
                }
                       
             }
             if (found == false)
                    System.out.println("Id does not exists.");
             
          } catch (Exception e) {
             e.printStackTrace();
          }
    }

}

The following output appears after executing the previous code. According to the output, the product ID of p-002 exists in the XML file. So, the content of the tag that contains this ID is printed.

Conclusion

The uses of the Java DOM parser to read the content of a simple XML file in different ways are shown in this tutorial using two examples. Many others parsers are available in Java to read or modify the XML documents.

Share Button

Source: linuxhint.com

Leave a Reply