Java Integration
Transform Anything with Java
Parsing XML without Namespaces using SAX
1. The first step is the create an XML file for parsing. Let us use the below XML file.
<?xml version="1.0" ?>
<items>
<item id="1">
<name>iPhone 5</name>
<manufacturer>Apple Inc</manufacturer>
<price>600</price>
</item>
<item id="2">
<name>iPhone 6</name>
<manufacturer>Apple Inc</manufacturer>
<price>700</price>
</item>
<item id="3">
<name>Moto X</name>
<manufacturer>Motorola</manufacturer>
<price>200</price>
</item>
<item id="4">
<name>Moto G</name>
<manufacturer>Motorola</manufacturer>
<price>450</price>
</item>
<item id="5">
<name>Samsung S4</name>
<manufacturer>Samsung Corp</manufacturer>
<price>550</price>
</item>
<items>
2. Open Eclipse and create a new Java Project. Also create a package named com.javatweaks.sax.read. We will create a class named Items to represent the xml.
package com.javatweaks.sax.read;
public class Items {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
private int id;
private String name;
private String manufacturer;
private String price;
public static final String
ID="id",
NAME="name",
MAN="manufacturer",
PRICE="price";
}
3. Now let us write the class that will use the SAX API's to read the XML named SAXItemsHandler. The class will extend DefaultHandler from the package org.xml.sax.helpers.DefaultHandler.
4. Let us define the below private variables.
private List<Items> data;
private Items item;
private String currentElement = "";
5. We are going to override 5 methods from SAX API.
-
startDocument() : represents the start of the document
-
endDocument() : represents the end of the document
-
startElement() : represents the start of the element
-
endElement() : represents the end of the element
-
characters() : to read the contents sequentially.
At first let me represent a the overridden metods without any implementation.
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
}
These are auto generated by eclipse. Type the method name and hit Ctrl + Space, Eclipse will give you options to auto complete the methods.
We will remove the calls to the super class and implement ourselves.
6. On the startDocument method, we will initialize a ArrayList of Items.
data = new ArrayList<>();
7. We will write just a System.out.Println("Document End at the ") on endDocument()
8. startElement() will have more implementation.
switch (currentElement) {
case "items":
break;
case "item":
item = new Items();
String id = attributes.getValue(Items.ID);
item.setId(Integer.parseInt(id));
data.add(item);
break;
default:
break;
}
}
9. endElement() will nullify the element variable so that we don't keep anything in the memory.
currentElement = "";
10. We have to write a method which will be the heart of the program(readDataFromXML). The method will return a list of Item and will take the file name as input.
We will create instances for SAXParserFactory and using that we will create an instance for SAXParser too. Below is the final program.
package com.javatweaks.sax.read;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
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 SAXItemsHandler extends DefaultHandler {
private List<Items> data;
private Items item;
private String currentElement = "";
private StringBuilder text;
public List<Items> readDataFromXML(String filename) throws SAXException,
IOException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File(filename), this);
return data;
}
@Override
public void startDocument() throws SAXException {
data = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = qName;
switch (currentElement) {
case "items":
break;
case "item":
item = new Items();
String id = attributes.getValue(Items.ID);
item.setId(Integer.parseInt(id));
data.add(item);
break;
default:
text = new StringBuilder();
break;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (currentElement.equals("items") || currentElement.equals("item")) {
System.out.println("endElement");
return;
}
String toPrint = text.toString();
switch (currentElement) {
case Items.MAN:
item.setManufacturer(toPrint);
break;
case Items.NAME:
item.setName(toPrint);
break;
case Items.PRICE:
item.setPrice(toPrint);
break;
default:
break;
}
currentElement = "";
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (text != null) {
text.append(ch, start, length);
}
}
}
11. Now let us create the main method to run the program and print the data. Name it as ReadItemsSAX
12. We will initialize the File object that we will read from . Also we will initialize SAXItemsHandler and make a call to the method readDatafromXML().
package com.javatweaks.sax.read;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class ReadItemsSAX {
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
String filename = "C:\\Users\\javatweaks\\Documents\\Java_XML\\Items.xml";
SAXItemsHandler saxHandler = new SAXItemsHandler();
List<Items> data = saxHandler.readDataFromXML(filename);
System.out.println(data.size());
for (Items items : data) {
System.out.println(items.getId() + ";" + items.getManufacturer()
+ ";" + items.getName() + ";" + items.getPrice());
}
}
}
The output will be as below.
5
1;Apple Inc;iPhone 5;600
2;Apple Inc;iPhone 6;700
3;Motorola;Moto X;200
4;Motorola;Moto G;450
5;Samsung Corp;Samsung S4;550