XML Parsing using XPath example
//Student.java
package com.jijo;
public class Student {
private String rollNo;
private String name;
private String className;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
//XPathRead.java
package com.jijo;
/**
* @author jijo
*
*/
import java.io.IOException;
import java.io.InputStream;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XPathRead {
private Document xmlDocument;
private XPath xPath;
public XPathRead(String xmlFile) {
initObjects(xmlFile);
}
public XPathRead(InputStream inputStream) {
initObjects(inputStream);
}
private void initObjects(String xmlFile){
try {
xmlDocument = DocumentBuilderFactory.
newInstance().newDocumentBuilder().
parse(xmlFile);
xPath = XPathFactory.newInstance().
newXPath();
} catch (IOException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
}
private void initObjects(InputStream inputStream){
try {
xmlDocument = DocumentBuilderFactory.
newInstance().newDocumentBuilder().
parse(inputStream);
xPath = XPathFactory.newInstance().
newXPath();
} catch (IOException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
}
public Object read(String expression,
QName returnType){
try {
XPathExpression xPathExpression =
xPath.compile(expression);
return xPathExpression.evaluate
(xmlDocument, returnType);
} catch (XPathExpressionException ex) {
ex.printStackTrace();
return null;
}
}
public Document getXmlDocument() {
return xmlDocument;
}
public void setXmlDocument(Document xmlDocument) {
this.xmlDocument = xmlDocument;
}
}
//XmlParser.java
/**
*
*/
package com.jijo;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author jijo
*
*/
public class XmlParser {
private static XPathRead reader;
public XmlParser(XPathRead xpathreader) {
this.reader=xpathreader;
}
public static XPathRead getXPathRead(String xmlData){
XPathRead pathReader = null;
try{
ByteArrayInputStream Bis1 = new ByteArrayInputStream(xmlData.getBytes("UTF-8"));
pathReader=new XPathRead(Bis1);
}
catch (Exception e) {
e.printStackTrace();
}
return pathReader;
}
public List<Student> getStudents(){
List<Student> students = new ArrayList<Student>();
try {
String expression = "/students/student"; //parsing expression
NodeList logCurves = (NodeList)reader.read(expression, XPathConstants.NODESET);
for(int index=0;index<logCurves.getLength();index++){
NodeList curveDetails = logCurves.item(index).getChildNodes();
Student student=new Student();
for(int curveItemIndex=0;curveItemIndex<curveDetails.getLength();curveItemIndex++){
if(curveDetails.item(curveItemIndex).getNodeType()!=Node.TEXT_NODE){
if(curveDetails.item(curveItemIndex).getNodeName().equals("rollno"))
student.setRollNo(curveDetails.item(curveItemIndex).getTextContent());
if(curveDetails.item(curveItemIndex).getNodeName().equals("name"))
student.setName(curveDetails.item(curveItemIndex).getTextContent());
if(curveDetails.item(curveItemIndex).getNodeName().equals("classname"))
student.setClassName(curveDetails.item(curveItemIndex).getTextContent());
}
}
students.add(student);
}
} catch (DOMException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
}
//Main.java
package com.jijo;
import java.util.List;
public class Main {
public static void main(String[] args) {
try{
/**
* using xpath
*/
XPathRead pathRead = new XPathRead("C:/jijo/xml/students.xml");
XmlParser parser = new XmlParser(pathRead);
List<Student> students = parser.getStudents();//contains all students data
}
catch (Exception e) {
e.printStackTrace();
}
}
}
XML is given
students.xml
<students>
<student>
<rollno>1</rollno>
<name>Jijo</name>
<classname>10</classname>
</student>
<student>
<rollno>2</rollno>
<name>Mathew</name>
<classname>10</classname>
</student>
<student>
<rollno>3</rollno>
<name>Ajeesh</name>
<classname>10</classname>
</student>
<student>
<rollno>4</rollno>
<name>Prathyush</name>
<classname>10</classname>
</student>
</students>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment