XML document processing in Java using XPath and XSLT

Slides:



Advertisements
Similar presentations
XPathAPI XPathAPI HOME : API : j/apidocs/ Lib.
Advertisements

XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
Internet Technologies 1 Lecture 1 Introduction.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
11-Jun-15 More DOM. Manipulating DOM trees DOM, unlike SAX, gives you the ability to create and modify XML trees There are a few roadblocks along the.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
MC365 XML Parsers. Today We Will Cover: An overview of the Java API’s used for XML processing Creating an XML document in Java Parsing an XML document.
XML, XPATH and JDOM ICW Lecture 9 Hasan Qunoo. Recap -XML document structure. -XML parsers. -JDOM: -Package Info. -Load An XML document. -Saving An XML.
JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Chapter 24 XML. CHAPTER GOALS Understanding XML elements and attributes Understanding the concept of an XML parser Being able to read and write XML documents.
Xpath Sources:
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
XML Android Club Agenda XML JSON XML eXtensible Markup Language Used for: data transfer PHP -> Java.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
Java WWW Week 10 Version 2.1 Mar 2008 Slide Java (JSP) and XML  Format of lecture: What is XML? A sample XML file… How to use.
Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen.
17 Apr 2002 XML Programming - DOM Andy Clark. DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
SDPLNotes 3.3: DOM Examples1 3.3 DOM Examples 1. DOM processing in a Web browser –a toy "XML database browser" –with JavaScript and MS Internet Explorer.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
SDPL 2002Notes 3.2: Document Object Model1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Consuming eXtensible Markup Language (XML) feeds.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Document Object Model DOM. Agenda l Introduction to DOM l Java API for XML Parsing (JAXP) l Installation and setup l Steps for DOM parsing l Example –Representing.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
SAX2 and DOM2 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Schema Data Processing
1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Web services. DOM parsing and SOAP.. Summary. ● Exercise: SAX-Based checkInvoice(), ● push parsing, ● event-based parsing, ● traversal order is depth-first.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
Chapter 26 XML. Chapter Goals Understanding XML elements and attributes Understanding the concept of an XML parser Being able to read and write XML documents.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Java API for XML Processing
XML. Contents  Parsing an XML Document  Validating XML Documents.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter 24 XML.
Hands on XSL XSL-basic elements.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
More DOM 13-Nov-18.
DOM Document Object Model.
XML Parsers By Chongbing Liu.
Jagdish Gangolly State University of New York at Albany
Java API for XML Processing
More DOM 28-Nov-18.
DOM 8-Dec-18.
WaysInJavaToParseXML
More DOM.
DOM 24-Feb-19.
Chapter 25 – XML.
XML Programming in Java
WaysInJavaToParseXML
Presentation transcript:

XML document processing in Java using XPath and XSLT

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.

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>

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<>();

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

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()); }

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); }

Exercises - Using the SAX parser, write and test the Xpaths 1. <document> <reference href="http://www.google.ro/"/> </document> Challenge Select the value of the attribute “href”. 2. <document xmlns="http://example.org"> <item_0001>Erstes Element</item_0001> <item_0002>Zweites Element</item_0002> <item_0003>Drittes Element</item_0003> <item_0004 xmlns="http://example.net/">Viertes 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”.

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.

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”.