Java XML Parsing Using DOM

In this tutorial you will learn about how to parse the xml file in java using DOM

Java XML Parsing Using DOM

Java XML Parsing Using SAX

To Parse XML document using SAX parser method you need to follow the following steps.

Consider the following XML file

Students.xml

<?xml version="1.0" encoding="UTF-8"?>
<Students>
    <Student type="regular">
       <Name>Joe</Name>
       <RollNo>21</RollNo>
       <Age>25</Age>
    </Student> 
    <Student type="open">
       <Name>Ramson</Name>
       <RollNo>23</RollNo>
       <Age>24</Age>
    </Student> 
</Students>

1. At first Create the SAX Parser as

// getting SAXParserFactory instance
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

// Getting SAXParser object from AXParserFactory instance
SAXParser saxParser = saxParserFactory.newSAXParser();

// Parsing XML Document by calling parse method of SAXParser class
saxParser.parse("C:\\Students.xml", this);

2. Override the startElement() methods of DefaultHandler
@Override
public void startElement(String uri, String localName, String qName,
	Attributes attributes) throws SAXException {
	value = "";
	if (qName.equalsIgnoreCase("Student")) {
		System.out.print(attributes.getValue("type") + "\t");
	}
}

3. Override the characters() methods of DefaultHandler
public void characters(char[] ch, int start, int length)
		throws SAXException {
	value = new String(ch, start, length);
}

4. Override the endElement() methods of DefaultHandler
public void endElement(String uri, String localName, String qName)
	throws SAXException {
	if (qName.equalsIgnoreCase("Name")) {
		System.out.print(value + "\t");
	}
}

A complete java class is given below based on above explanation

XmlParsingUsingSax.java

package net.roseindia;

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 XmlParsingUsingSax extends DefaultHandler {

	private String value;

	public static void main(String[] args) {
		System.out.println("Type\tName\tRollNo\tAge\n");
		new XmlParsingUsingSax().parseXmlDocument();
	}

	public void parseXmlDocument() {
		try {
			// getting SAXParserFactory instance
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

			// Getting SAXParser object from AXParserFactory instance
			SAXParser saxParser = saxParserFactory.newSAXParser();

			// Parsing XML Document by calling parse method of SAXParser class
			saxParser.parse("C:\\Students.xml", this);

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

	// Overriding characters() method of DefaultHandler class
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		value = new String(ch, start, length);
	}

	// Overriding startElement() method of DefaultHandler class
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		value = "";
		if (qName.equalsIgnoreCase("Student")) {
			System.out.print(attributes.getValue("type") + "\t");
		}
	}

	// Overriding endElement() method of DefaultHandler class
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("Name")) {
			System.out.print(value + "\t");
		}

		if (qName.equalsIgnoreCase("RollNo")) {
			System.out.print(value + "\t");
		}
		if (qName.equalsIgnoreCase("Age")) {
			System.out.print(value + "\n");
		}
	}
}


When you run this application it will display message as shown below:


Type Name RollNo Age

regular Joe 21 25
open Ramson 23 24

Download Select Source Code