XPath is a W3C recommendation.
XPath is a language to navigate through different parts of xml document,designed to be used bu both XSLT and XPointer.
Simple way to use XPath with java .
The XML file need to read : hello.xml
XPath is a language to navigate through different parts of xml document,designed to be used bu both XSLT and XPointer.
Simple way to use XPath with java .
The XML file need to read : hello.xml
And , Hear is the java code to read the file named : hello.xmlAsraful 24 Male Rid 22 Male Tina 19 Female
package com.thinktank.rnd.se.pattern.xml; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; public class XMLReadWrite { public static void main(String args[]) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse("hello.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//person/*/text()"); Object result = expr.evaluate(document, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } } }Finally Output of the code :
Asraful
24
Male
Rid
22
Male
Tina
19
Female
No comments:
Post a Comment