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 document. -Element/Attributes Operations. -Element Operations. -Demo
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.
XML Example - Revisited <?xml version="1.0“ encoding="UTF-8"?> red white blue green circle square
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 Selects attributes
XPATH Expressions Examples: //root/shapes //colors/color[1]/text() //root/colors //root/color shapes <?xml version="1.0“ encoding="UTF-8"?> red white blue green circle square
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.
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??!!
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);
Example -Find a node with XPATH query. -Delete node using XPATH query.
XSLT - XSLT stands for EXtensible Stylesheet Language Transformation. - XML has the data. How can I present it.
Example
Next Lecture XSLT and Java Transformers.