Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

XML Parsing in Java: A Beginner’s Guide

XML (Extensible Markup Language) is a widely used language for data exchange between different systems. It is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML documents are structured as a tree of elements, where each element has a name, attributes, and child elements. In this article, we will explore how to parse XML documents in Java.

What is XML Parsing?

XML parsing is the process of reading an XML document and extracting the information it contains. The Java platform provides several APIs for Parsing XML documents, including DOM (Document Object Model), SAX (Simple API for XML), and StAX (Streaming API for XML).

DOM Parsing in Java

DOM parsing is a tree-based parsing approach that builds an in-memory representation of the entire XML document. The DOM API provides a set of classes and methods for navigating and manipulating the document tree. Here is an example of how to parse an XML document using DOM in Java:

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 DomParserExample {
   public static void main(String[] args) {

      try {
         File inputFile = new File("input.xml");
         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");

         for (int temp = 0; temp 

In this example, we are parsing an XML document that contains information about students. We first create a DocumentBuilderFactory object and use it to create a DocumentBuilder object. We then use the DocumentBuilder object to parse the XML document and create a Document object. We then normalize the document by removing any empty text nodes and combining adjacent text nodes. We print the root element of the document and then get a NodeList of all the student elements in the document. We loop through the NodeList and extract the information for each student element. We use the Element.getAttribute() method to get the value of the rollno attribute, and we use the Element.getElementsByTagName() method to get the values of the child elements.

SAX Parsing in Java

SAX parsing is an event-based parsing approach that reads an XML document sequentially and triggers events for each element, attribute, and text node. The SAX API provides a set of interfaces and methods for handling these events. Here is an example of how to parse an XML document using SAX in Java:

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParserExample {

   public static void main(String[] args) {

      try {
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();

         DefaultHandler handler = new DefaultHandler() {

            boolean bfname = false;
            boolean blname = false;
            boolean bnname = false;
            boolean bmarks = false;

            public void startElement(String uri, String localName,String qName, 
                Attributes attributes) throws SAXException {

               System.out.println("Start Element :" + qName);

               if (qName.equalsIgnoreCase("FIRSTNAME")) {
                  bfname = true;
               }

               if (qName.equalsIgnoreCase("LASTNAME")) {
                  blname = true;
               }

               if (qName.equalsIgnoreCase("NICKNAME")) {
                  bnname = true;
               }

               if (qName.equalsIgnoreCase("MARKS")) {
                  bmarks = true;
               }
            }

            public void endElement(String uri, String localName,
                String qName) throws SAXException {

               System.out.println("End Element :" + qName);

            }

            public void characters(char ch[], int start, int length) throws SAXException {

               if (bfname) {
                  System.out.println("First Name : " + new String(ch, start, length));
                  bfname = false;
               }

               if (blname) {
                  System.out.println("Last Name : " + new String(ch, start, length));
                  blname = false;
               }

               if (bnname) {
                  System.out.println("Nick Name : " + new String(ch, start, length));
                  bnname = false;
               }

               if (bmarks) {
                  System.out.println("Marks : " + new String(ch, start, length));
                  bmarks = false;
               }

            }

         };

         saxParser.parse("input.xml", handler);

      } catch (Exception e) {
         e.printStackTrace();
      }

   }

}

In this example, we are parsing the same XML document as before, but using the SAX API. We first create a SAXParserFactory object and use it to create a SAXParser object. We then create a DefaultHandler object and override its methods to handle the events triggered by the SAX parser. We use boolean flags to keep track of which element we are currently processing. In the startElement() method, we set the appropriate flag based on the name of the element. In the characters() method, we print the value of the element if the corresponding flag is set. In the endElement() method, we print the name of the element.

StAX Parsing in Java

StAX parsing is a stream-based parsing approach that reads an XML document sequentially and allows for both reading and writing of XML documents. The StAX API provides a set of interfaces and methods for handling XML events. Here is an example of how to parse an XML document using StAX in Java:

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.io.FileInputStream;

public class StaxParserExample {

   public static void main(String[] args) {

      try {
         XMLInputFactory factory = XMLInputFactory.newInstance();
         XMLStreamReader reader = 
            factory.createXMLStreamReader(new FileInputStream("input.xml"));

         while (reader.hasNext()) {
            int event = reader.next();

            switch (event) {
               case XMLStreamConstants.START_ELEMENT:
                  System.out.println("Start Element :" + reader.getName());
                  break;

               case XMLStreamConstants.END_ELEMENT:
                  System.out.println("End Element :" + reader.getName());
                  break;

               case XMLStreamConstants.CHARACTERS:
                  System.out.println("Text : " + reader.getText());
                  break;

               case XMLStreamConstants.ATTRIBUTE:
                  System.out.println("Attribute : " + reader.getAttributeName());
                  break;

               case XMLStreamConstants.START_DOCUMENT:
                  System.out.println("Start Document");
                  break;

               case XMLStreamConstants.END_DOCUMENT:
                  System.out.println("End Document");
                  break;
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

In this example, we are parsing the same XML document as before, but using the StAX API. We first create an XMLInputFactory object and use it to create an XMLStreamReader object. We then loop through the events triggered by the reader and print the appropriate information for each event.

Conclusion

In this article, we explored how to parse XML documents in Java using three different APIs: DOM, SAX, and StAX. Each approach has its own strengths and weaknesses, and the choice of which one to use depends on the specific requirements of your application. XML parsing is an important skill for any Java developer, and mastering it can open up a world of possibilities for data exchange and integration.

Related Articles

  • JDBC vs. ORM: Which One to Choose for Your Java Project?
  • Creating a Real-Time Chat Application with Java and WebSockets
  • Creating a RESTful API with Jersey
  • Securing Your Java Web Application with Spring Security
  • Building a Web Application with Spring Boot

The post XML Parsing in Java: A Beginner’s Guide appeared first on Java Master.



This post first appeared on Java Master, please read the originial post: here

Share the post

XML Parsing in Java: A Beginner’s Guide

×

Subscribe to Java Master

Get updates delivered right to your inbox!

Thank you for your subscription

×