XML SAX Parser in Java

The same simple xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person>
		<name>debraj</name>
		<age unit="year">23</age>
		<sex>male</sex>
		<location>india</location>
	</person>
	<person>
		<name>ritu</name>
		<age unit="year">33</age>
		<sex>female</sex>
		<location>india</location>
	</person>
</persons>

The SAX parsing

private List<Person> list;
private Person p;
private String tmpValue;

public List<Person> parse(InputStream is) {
	list = new ArrayList<Person>();
	SAXParserFactory spf = SAXParserFactory.newInstance();
	SAXParser sp = spf.newSAXParser();
	sp.parse(is, this);
	
	return list;
}

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
	if (qName.equalsIgnoreCase("person")) {
		tmpValue = "";
		p = new Person();
	}
}

public void characters(char ch[], int start, int length) throws SAXException {
	tmpValue = new String(ch, start, length);
}

public void endElement(String uri, String localName, String qName) throws SAXException {
	if (qName.equalsIgnoreCase("name")) {
		p.setName(tmpValue);
	}
	if (qName.equalsIgnoreCase("age")) {
		p.setAge(Integer.parseInt(tmpValue));
	}
	if (qName.equalsIgnoreCase("sex")) {
		p.setSex(tmpValue);
	}
	if (qName.equalsIgnoreCase("location")) {
		p.setLocation(tmpValue);
	}

	if (qName.equalsIgnoreCase("person")) {
		list.add(p);
	}
}

http://www.java-samples.com/showtutorial.php?tutorialid=152

Advertisements

XML StAX Parser in Java

The same simple xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person>
		<name>debraj</name>
		<age unit="year">23</age>
		<sex>male</sex>
		<location>india</location>
	</person>
	<person>
		<name>ritu</name>
		<age unit="year">33</age>
		<sex>female</sex>
		<location>india</location>
	</person>
</persons>

and the StAX parsing

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(is);

while (streamReader.hasNext()) {
	streamReader.next();
	if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
		if (streamReader.getLocalName() == "person") {
			Person p = new Person();
			while (streamReader.hasNext()) {
				streamReader.next();
				if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
					if (streamReader.getLocalName() == "name") {
						p.setName(streamReader.getElementText());
					}
					if (streamReader.getLocalName() == "age") {
						p.setAge(Integer.parseInt(streamReader.getElementText()));
					}
					if (streamReader.getLocalName() == "sex") {
						p.setSex(streamReader.getElementText());
					}
					if (streamReader.getLocalName() == "location") {
						p.setLocation(streamReader.getElementText());
						break;
					}
				}
			}
		}
	}
}

http://tutorials.jenkov.com/java-xml/stax-xmlstreamreader.html
http://javaboutique.internet.com/tutorials/stax/index3.html

XML Dom Parser in Java

A simple XML

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person>
		<name>debraj</name>
		<age unit="year">23</age>
		<sex>male</sex>
		<location>india</location>
	</person>
	<person>
		<name>ritu</name>
		<age unit="year">33</age>
		<sex>female</sex>
		<location>india</location>
	</person>
</persons>

The parsing

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser;

parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(is));

NodeList persons = doc.getElementsByTagName("persons");
NodeList personList = persons.item(0).getChildNodes();

for (int i = 0; i < personList.getLength(); i++) {
	Node person = personList.item(i);

	if (person.getNodeType() == Node.ELEMENT_NODE) {
		NodeList personDetail = person.getChildNodes();
		Person p = new Person();
		for (int j = 0; j < personDetail.getLength(); j++) {
			Node n = personDetail.item(j);

			if (n.getNodeType() == Node.ELEMENT_NODE) {

				if (n.getNodeName() == "name") {
					p.setName(n.getTextContent());
				}
				if (n.getNodeName() == "age") {
					p.setAge(Integer.parseInt(n.getTextContent()));
				}
				if (n.getNodeName() == "sex") {
					p.setSex(n.getTextContent());
				}
				if (n.getNodeName() == "location") {
					p.setLocation(n.getTextContent());
				}
			}
		}
	}
}