Simple DOM example Java

1. The DOM parser example:

package com.vvirlan;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomExample {
	public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
		DomExample ex = new DomExample();
		ex.run();
	}

	public void run() throws SAXException, IOException, ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		// factory.setValidating(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(new File("C:\\tmp\\myxml.xml"));
		NodeList nodeList = doc.getElementsByTagName("name");
		Node firstNode = nodeList.item(0);
		System.out.println(firstNode.getNodeName() + " = " + firstNode.getTextContent());
	}
}

2. The XML file

<?xml version="1.0" encoding="UTF-8"?>
<records>
	<record>
		<name>Queen I</name>
		<year>1988</year>
		<artist>Queen</artist>
	</record>
		<record>
		<name>Reloaded</name>
		<year>1992</year>
		<artist>Metallica</artist>
	</record>
</records>

Simple SAX example in Java

1. SaxExample class

package com.vvirlan;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class SaxExample {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		SaxExample ex = new SaxExample();
		ex.run();
	}

	public void run() throws ParserConfigurationException, SAXException, IOException {

		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setNamespaceAware(true);
		SAXParser parser = factory.newSAXParser();
		XMLReader xmlReader = parser.getXMLReader();
		xmlReader.setContentHandler(new MyContentHandler());

		FileInputStream fis = new FileInputStream(new File("C:\\tmp\\myxml.xml"));
		InputSource source = new InputSource(fis);
		xmlReader.parse(source);
	}
}

2. MyHandler

package com.vvirlan;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class MyContentHandler implements ContentHandler {

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("Documemt Started");

	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("Documemt Ended");

	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		System.out.println("element Started "+localName);

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println("element ended "+localName);

	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("Characters: "+new String(ch).substring(start, start+length));

	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String target, String data) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}

}

3. The source XML file

<?xml version="1.0" encoding="UTF-8"?>
<records>
	<record>
		<name>Queen I</name>
		<year>1988</year>
		<artist>Queen</artist>
	</record>
		<record>
		<name>Reloaded</name>
		<year>1992</year>
		<artist>Metallica</artist>
	</record>
</records>