Home Java Example Java XmlParsing Java XML Parsing Using SAX
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

Java XML Parsing Using SAX

Advertisement
In this tutorial you will learn how to parse xml file using SAX parser in java

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 5, 2011

Related Tutorials

Ask Questions?    Discuss: Java XML Parsing Using SAX  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com