Xpath Sources:

Slides:



Advertisements
Similar presentations
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Advertisements

XPath Eugenia Fernandez IUPUI. XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing.
Lecture 13. The various node tests also work on this axis: eg node() This book has descendant-or- self nodes As expected, text nodes are included in 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.
XSL Transformations Lecture 8, 07/08/02. Templates The whole element is a template The match pattern determines where this template applies Result element(s)
1 Processing XML with Java CS , Spring 2008/9.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
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.
XPath Tao Wan March 04, What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
1 XML Data Management 4. Domain Object Model Werner Nutt.
Navigating XML. Overview  Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is  to address parts of an.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
Xpath Xlink Xpointer Xquery Sources:
1/17 ITApplications XML Module Session 7: Introduction to XPath.
CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML)‏
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.
XPath XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer.
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
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.
Processing of structured documents Spring 2003, Part 7 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
The Main Routine In these notes I call it DataImporter. It contains: A main routine the creates an instance of IndexerData, the root of our data model.
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.
TA. Min-Joong Lee x7837)
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.
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.
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.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Session II Chapter 3 – Chapter 3 – XPath Patterns & Expressions Chapter 4 – XPath Functions Chapter 15 – XPath 2.0http://
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
What is it? –w3c standard for… Navigating through an XML document Filtering & finding information –Includes… Expressions for navigating to parts of an.
XML. RHS – SOC 2 Data vs. Information We often use the terms data and information interchangeably More precisely, data is some ”value” of a certain type,
1 XPath. 2 Agenda XPath Introduction XPath Nodes XPath Syntax XPath Operators XPath Q&A.
Files and Serialization. Files Used to transfer data to and from secondary storage.
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.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
XSLT in Practice. Exercises  download Apache Xalan - install it - try the example in Xalan-Java Overview  ZVON XSLT Tutorial.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Hands on XSL XSL-basic elements.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter 16 Iterators.
Accessing Files in Java
XML stands for Extensible Markup Language.
Chapter 7 Iterators.
WaysInJavaToParseXML
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
DOM 24-Feb-19.
Podcast Ch23f Title: Serialization
XML document processing in Java using XPath and XSLT
The command invocation protocol
WaysInJavaToParseXML
Presentation transcript:

Xpath Sources:

Overlapping domains

XPath XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C Standard

Terminology Element Attribute text, namespace, processing-instruction, comment, document (root) nodes

expressions The most useful path expressions: nodename Selects all child nodes of the named node / Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are. Selects the current node.. Selects the parent of the current Selects attributes

Wildcards Path wildcards can be used to select unknown XML elements. * Matches any element Matches any attribute node node() Matches any node of any kind

Axis: a node-set relative to the current node.

Operators

Xpath functions See tions.asp

/ matches the root node A matches any element * matches any element A|B matches any or element A/B matches any element within a element A//B matches any element with a ancestor text() matches any text node Pattern Matching - nodes

id(“pippo”) matches the element with unique ID pippo A[1] matches any element that is the first child of its parent A[last()=1] matches any element that is the last child of its parent B/A[position() mod 2 = 1] matches any element that is an odd-numbered child of its B parent Pattern Matching

@A matches any A matches any attribute matches any element that has a ancestor with a A attribute with v value processing-instruction() node() Pattern Matching - attributes

Using Xpath from java XPath expressions are much easier to write than detailed (DOM) navigation code. When you need to extract information from an XML document, the quickest and simplest way is to embed an XPath expression inside your Java program. Java 5 introduces the javax.xml.xpath package, an XML object-model independent library for querying documents with XPath.

Example Find all the books by Dante Alighieri //book[author="Dante Alighieri"]/title assuming a suitable data structure: … … Title of the book … …

Java code 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 { //read an XML file into a DOM Document DocumentBuilderFactory domFactory= DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = domFactory.newDocumentBuilder();Document doc = builder.parse("books.xml");

Java code // prepare the XPath expression XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//book[author='Dante Alighieri']/title/text()"); // evaluate the expression on a Node Object result = expr.evaluate(doc, XPathConstants.NODESET); // examine the results NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }

XML Serialization

import java.io.File; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.load.Persister; public class StorableAsXML implements Serializable { // ==== SERIALIZATION/DESERIALIZATION PRIMITIVES public void persist(File f){ Serializer serializer = new Persister(); try { serializer.write(this, f); } catch (Exception ex) { ex.printStackTrace(); } Using XML to serialize Java Classes import java.io.Serializable; public class X implements Serializable FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos=new FileOutputStream(f); oos = new ObjectOutputStream(fos); oos.writeObject(this); } catch (IOException ex) { ex.printStackTrace(); }

public StorableAsXML resume(File f, Class c){ StorableAsXML retval = null; try { Serializer serializer = new Persister(); retval = (StorableAsXML)serializer.read(c, f); } catch (Exception ex) { ex.printStackTrace(); } return retval; } Using XML to serialize Java Classes FileInputStream fis=null; ObjectInputStream ois=null; try { fis=new FileInputStream(f); ois = new ObjectInputStream(fis); retval=(X)ois.readObject(); } catch (Exception ex) { ex.printStackTrace(); }

public class Lecture extends StorableAsXML implements Serializable { private Set lecturers=null; //non serialized public String private Date private int sequenceNumber=-1; //-1 means not private String courseRef=null; //Home per il private String private String private String private String videoLenght=null; //null = Video does not private boolean hasPostProcessing=false; Using XML to serialize Java Classes public Lecture(){ // needed to be a bean //for XMLSerialization …; }

Generated XML gg :20: CEST 1 /Users/ronchet/_LODE/COURSES/Hh_ _Gg_ A.B. false

@Root(name="COURSE") public class Course extends StorableAsXML implements Serializable private String private String private String private Set lectures=new TreeSet private Set teachers=new TreeSet (); Using XML to serialize Java Classes hh 2008 /Hh_ _Gg_ A.B. C.D.

Javadoc m/doc/javadoc/org/simpleframework/xml/p ackage-summary.html