Download presentation
Presentation is loading. Please wait.
1
XML, XPATH and JDOM ICW Lecture 9 Hasan Qunoo
2
Recap -XML document structure. -XML parsers. -JDOM: -Package Info. -Load An XML document. -Saving An XML document. -Element/Attributes Operations. -Element Operations. -Demo
3
XPATH -XPATH stands for XML Path Language. -XPATH is a query language for selecting nodes from an XML document. -XPATH is a subset of XQUERY (a more advanced XML query language). -XPath uses path expressions to select nodes or node-sets in an XML document.
4
XML Example - Revisited <?xml version="1.0“ encoding="UTF-8"?> red white blue green circle square
5
XPATH - Syntax 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 node @ Selects attributes
6
XPATH Expressions Examples: //root/shapes //colors/color[1]/text() //root/colors //root/shapes[@size>2] //root/color shapes //shape[@size>2] <?xml version="1.0“ encoding="UTF-8"?> red white blue green circle square
7
JDOM with XPATH Expressions -We import the class: -import org.jdom.xpath.XPath; We can use the method: List XPath.selectNodes( ). N: is a node in the XML document. Usually the root node. X: is the XPATH expression as String object.
8
JDOM with XPATH Expressions -Also, we can use the method: Object X path.selectSingleNode( ). N: is a node in the XML document. Usually the root node. X: is the XPATH expression as String object. Returns a the first entry in the result. Remember to cast the object before use. Note: Anything Missing??!!
9
JDOM with XPATH Expressions -Alternatively, Create a XPathFactory: XpathFactory xFactory = PathFactory.newInstance(); -Create a XPath object: XPath xpath =xFactory.newXPath(); -Compile the XPath expression XPathExpression expr = xpath.compile("//colors[color=‘red‘]/text()"); -Run the query and get a nodeset Object result = expr.evaluate(doc, XPathConstants.NODESET);
10
Example -Find a node with XPATH query. -Delete node using XPATH query.
11
XSLT - XSLT stands for EXtensible Stylesheet Language Transformation. - XML has the data. How can I present it.
12
Example
13
Next Lecture XSLT and Java Transformers.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.