Archives: XML

Java – Read XML File – SAX Parser

SAX Parser is faster and uses less memory than DOM parser.

file.xml
[code lang=”xml”]<?xml version="1.0"?>
<company>
<staff>
<firstname>prasanna</firstname>
<lastname>sherekar</lastname>
<nickname>pashyaa</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>james</firstname>
<lastname>anderson</lastname>
<nickname>lolo</nickname>
<salary>200000</salary>
</staff>
</company>[/code]
ReadXMLFile.java
[code lang=”java”]
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 ReadXMLFile {

public static void main(String argv[]) {

try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;

public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {

System.out.println("Start Element :" + qName);

if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
}

if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}

if (qName.equalsIgnoreCase("NICKNAME")) {
bnname = true;
}

if (qName.equalsIgnoreCase("SALARY")) {
bsalary = true;
}

}

public void endElement(String uri, String localName,
String qName) throws SAXException {

System.out.println("End Element :" + qName);

}

public void characters(char ch[], int start, int length) throws SAXException {

if (bfname) {
System.out.println("First Name : " + new String(ch, start, length));
bfname = false;
}

if (blname) {
System.out.println("Last Name : " + new String(ch, start, length));
blname = false;
}

if (bnname) {
System.out.println("Nick Name : " + new String(ch, start, length));
bnname = false;
}

if (bsalary) {
System.out.println("Salary : " + new String(ch, start, length));
bsalary = false;
}

}

};

saxParser.parse("c:\\file.xml", handler);

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

}

}
[/code]


Java – Read XML File – DOM Parser

File.xml
[code lang=”xml”]<?xml version="1.0"?>
<company>
<staff>
<firstname>prasanna</firstname>
<lastname>sherekar</lastname>
<nickname>pashyaa</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>james</firstname>
<lastname>anderson</lastname>
<nickname>lolo</nickname>
<salary>200000</salary>
</staff>
</company>[/code]
ReadXMLFile.java
[code lang=”java”]import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

public static void main(String argv[]) {

try {

File fXmlFile = new File("c:\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("———————–");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

System.out.println("First Name : " + getTagValue("firstname", eElement));
System.out.println("Last Name : " + getTagValue("lastname", eElement));
System.out.println("Nick Name : " + getTagValue("nickname", eElement));
System.out.println("Salary : " + getTagValue("salary", eElement));

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

private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

Node nValue = (Node) nlList.item(0);

return nValue.getNodeValue();
}

}[/code]
Output:

Root element :company
———————–
First Name : prasanna
Last Name : sherekar
Nick Name : pashyaa
Salary : 500000
First Name : james
Last Name : anderson
Nick Name : james
Salary : 200000