Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML document processing in Java using XPath and XSLT

Similar presentations


Presentation on theme: "XML document processing in Java using XPath and XSLT"— Presentation transcript:

1 XML document processing in Java using XPath and XSLT

2 What is DOM? What is SAX? Document Object Model (DOM) = provides a standard interface for working with an XML document in a tree hierarchy. Simple API for XML (SAX) = lets a program parse an XML document sequentially, based on an event handling model. Both represent APIs.

3 DOM parser The DOM Parser loads the complete XML content into a Tree structure. The iteration is done through the Node and NodeList to get the content of the XML. The XML sample for parsing using DOM parser: <students> <student id="1"> <firstName nickname="Aga">Agamemnon</firstName> <lastName>Dandanache</lastName> <location>Romania</location> </student> <student id="2"> <firstName nickname="Ferro">Ferrero</firstName> <lastName>Rocher</lastName> <location>Italy</location> <student id="3"> <firstName nickname="Leana">Ileana</firstName> <lastName>Cosanzeana</lastName> <location>FairyTaleLand</location> </students>

4 DOM Parser code (i) package seweblab4; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMParser { public static void main(String[] args) throws Exception { //Get the DOM Builder Factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Get the DOM Builder DocumentBuilder builder = factory.newDocumentBuilder(); //Load and Parse the XML document //document contains the complete XML as a Tree. Document document = builder.parse(ClassLoader.getSystemResourceAsStream("students.xml")); List<Student> stuList = new ArrayList<>();

5 DOM Parser code (ii) Agamemnon Dandanache(1)Romania
//Iterating through the nodes and extracting the data. NodeList nodeList = document.getDocumentElement().getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { //We have encountered a <student> tag. Node node = nodeList.item(i); if (node instanceof Element) { Student stu = new Student(); stu.id = node.getAttributes().getNamedItem("id").getNodeValue(); NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node cNode = childNodes.item(j); //Identifying the child tag of student encountered. if (cNode instanceof Element) { String content = cNode.getLastChild().getTextContent().trim(); switch (cNode.getNodeName()) { case "firstName": stu.firstName = content; break; case "lastName": stu.lastName = content; break; case "location": stu.location = content; break; }} } stuList.add(stu); }} for (Student s : stuList) { System.out.println(s); }}} Agamemnon Dandanache(1)Romania Ferrero Rocher(2)Italy Ileana Cosanzeana(3)FairyTaleLand

6 Sax Parser – Part 1 import java.io.IOException; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import javax.xml.xpath.*; public class XPathExample { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("students.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("/students/student/firstName/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }

7 Sax Parser – Part 2 expr = xpath.compile("count(/students/student)"); Double count = (Double) expr.evaluate(doc, XPathConstants.NUMBER); System.out.println("Students no = " + count); //Is there any person named Agamemnon? expr = xpath.compile("count(/students/student[firstName='Agamemnon']) > 0"); Boolean res = (Boolean) expr.evaluate(doc, XPathConstants.BOOLEAN); System.out.println(res); //get the attribute value of firstName expr = xpath.compile("/students/student/firstName"); result = expr.evaluate(doc, XPathConstants.NODESET); nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { Node nNode = nodes.item(i); String atrNick = nNode.getAttributes().getNamedItem("nickname").getNodeValue(); System.out.println(atrNick); }

8 Exercises - Using the SAX parser, write and test the Xpaths
1. <document> <reference href=" </document> Challenge Select the value of the attribute “href”. 2. <document xmlns=" <item_0001>Erstes Element</item_0001> <item_0002>Zweites Element</item_0002> <item_0003>Drittes Element</item_0003> <item_0004 xmlns=" Element</item_0004> <other_0001>Erstes Element</other_0001> <other_0002>Zweites Element</other_0002> </document> Select all elements having a name beginning with “item”. 3. <jobs> <job priority="critical" name="Müll rausbringen" /> <job priority="low" name="Möbel säubern" /> <job priority="low" name="Teppich reinigen" /> <job priority="medium" name="Fenster putzen" /> <job priority="high" name="Pflanzen gießen" /> </jobs> Output the number of jobs with the priority “low”.

9 Exercises - Using the SAX parser, write and test the Xpaths
4. <person> <lastName>Peter</lastName> <firstName>Hans</firstName> </person> Create an output consisting of the elements “lastName”, the character string ", " and firstName. 5. <persons> <person firstName="Hans" lastName="Mustermann" age="28" /> <person firstName="Herbert" lastName="Möllemann" age="33" /> <person firstName="Peter" lastName="Meier" age="37" /> <person firstName="Ulrike" lastName="Albrecht" age="45" /> </persons> Select all persons aged less than 35 years. 6. Select all persons whose first name begins with the letter H. 7. Select all person elements with an attribute “firstName” a maximum of 5 characters long.

10 Exercises - Using the SAX parser, write and test the Xpaths
8. <collection> <artist> <name>Robbie Williams</name> <cds> <cd>Rudebox</cd> <cd>Swing when you're winning</cd> </cds> </artist> <band> <name>Juli</name> <cds> <cd>Ein neuer Tag</cd> </cds> </band> <band> <name>Silbermond</name> <cds> <cd>Verschwende deine Zeit</cd> </cds> </band> <artist> <name>Michael Jackson</name> <cds> <cd>Bad</cd> <cd>Thriller</cd> </cds> </artist> </collection> Output how many albums in the collection are by solo artists (artist) and how many are by bands (band). 9. Select the name of the artist that goes with the CD “Rudebox”. 10. Output the name of the first-listed CD by the last artist in the list. => Result: “Bad”.


Download ppt "XML document processing in Java using XPath and XSLT"

Similar presentations


Ads by Google