Apr 042018

ADM

How to read XML file in Java using SAX Parser

  • 04 April 2018
  • ADM

 

 

 

How to read XML file in Java using SAX Parser - images/logos/java.jpg

 

In this tutorial will use the output from How to create XML file in java using DOM tutorial plus adding a few more elements.

XML file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
  <user id="100">
    <name>John Doe</name>
    <email>john.doe@example.com</email>
  </user>
  <user id="200">
    <name>Richard Bo</name>
    <email>richard.bo@example.com</email>
  </user>
  <user id="300">
    <name>Ant Sam</name>
    <email>ant.sam@example.com</email>
  </user>
</users>

Code

package com.admfactory.xml;

import java.util.HashMap;

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 XMLSaxReader {
    
    public static void main(String[] args) {
	try {
	    System.out.println("XML Parser using SAXParser Example");
	    System.out.println();
	    
	    SAXParserFactory factory = SAXParserFactory.newInstance();
	    SAXParser saxParser = factory.newSAXParser();
	    
	    /** defining the custom handler */
	    DefaultHandler handler = new DefaultHandler() {
		
		HashMap<String, Boolean> elements = new HashMap<String, Boolean>();
		
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		    System.out.println("Start Element :" + qName);
		    /** storing the fact that the element has started */
		    elements.put(qName, true);
		}
		
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
		    System.out.println("End Element :" + qName);
		    
		    /** this section is just to add some space in the console print out */
		    if (qName.equalsIgnoreCase("user")) {
			System.out.println();
		    }
		}

		@Override
		public void characters(char[] characters, int start, int length) throws SAXException {
		    
		    if (elements.containsKey("name") && elements.get("name")) {
			System.out.println("Name: " + new String(characters, start, length));
			elements.put("name", false);
		    }
		    if (elements.containsKey("email") && elements.get("email")) {
			System.out.println("Email: " + new String(characters, start, length));
			elements.put("email", false);
		    }
		}
		
	    };
	    
	    saxParser.parse("d:\\admfactory.com\\users.xml", handler);

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

Output

XML Parser using SAXParser Example

Start Element :users
Start Element :user
Start Element :name
Name: John Doe
End Element :name
Start Element :email
Email: john.doe@example.com
End Element :email
End Element :user

Start Element :user
Start Element :name
Name: Richard Bo
End Element :name
Start Element :email
Email: richard.bo@example.com
End Element :email
End Element :user

Start Element :user
Start Element :name
Name: Ant Sam
End Element :name
Start Element :email
Email: ant.sam@example.com
End Element :email
End Element :user

End Element :users

 

 

 

References