XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Xpath

XML Xpath

shape Description

XPath is an query dialect that is utilized for crossing through a XML report. It is utilized ordinarily to hunt specific components or properties with coordinating examples. This instructional exercise clarifies the fundamentals of XPath. It contains sections examining all the fundamental segments of XPath with appropriate illustrations. If there should arise an occurrence of HTML archives, labels are predefined, for example, table, div, traverse, and so forth. The program knows how to add style to them and show them utilizing CSS styles. In any case, if there should arise an occurrence of XML records, labels are not predefined. With a specific end goal to comprehend and style an XML report, World Wide Web Consortium (W3C) created XSL which can go about as an XML-based Stylesheet Language. An XSL archive indicates how a program ought to render an XML report.

XML XPath Syntax

shape Description

A XPath expression is assessed regarding a setting hub. An Axis Specifier, for example, "youngster" or "relative" determines the bearing to explore from the setting hub. The hub test and the predicate are utilized to channel the hubs determined by the hub specifier: For instance, the hub test "A" requires that all hubs explored to must have named 'A'. A predicate can be utilized to determine that the chose hubs have certain properties, which are indicated by XPath expressions themselves. The XPath language structure comes in two flavors: the truncated grammar is smaller and permits XPaths to be composed and read effortlessly utilizing instinctive and, much of the time, recognizable characters and develops. The full language structure is more verbose, yet takes into account more alternatives to be determined, and is clearer if read deliberately.

Abbreviated syntax

The smaller documentation permits many defaults and truncations for normal cases. Given source XML containing in any event. [xml]<A> <B> <C/> </B> </A>[/xml]

shape Example

The following is an example. XPathParserDemo.java [xml]package com.splessons; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.xml.sax.SAXException; public class XPathParserDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "/class/student"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Node nNode = nodeList.item(i); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Student roll no : " + eElement.getAttribute("rollno")); System.out.println("First Name : " + eElement .getElementsByTagName("firstname") .item(0) .getTextContent()); System.out.println("Last Name : " + eElement .getElementsByTagName("lastname") .item(0) .getTextContent()); System.out.println("Nick Name : " + eElement .getElementsByTagName("nickname") .item(0) .getTextContent()); System.out.println("Marks : " + eElement .getElementsByTagName("marks") .item(0) .getTextContent()); } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } } }[/xml] The parser is a program part which represents the physical form of a data and converts it into storage form for the program. The one which reads XML Data in an XML file is referred to as XML Parser. Until unless the code is copied as a unit block blindly, every XML program should call the XML Parser. The world working in XML with Java is not an alphabet soup of acronyms such as DOM, JDOM, JAXP and so on. One of the frequently used JavaXML Parsers is JAXP which stands for Java API for XML Processing that describes the standards for the XML API’s that are included in Java SE-like. Document Object Model defines a set of interfaces to the parsed version of an XML document. The parser reads the entire document and builds an in-memory tree. DOM is platform browser and language neutral. It does not assume anything about what platform is running on what browser turning on and there are several language implementations of the DOM that can be used to work with. One of the most common languages that people work with the DOM is JavaScript. Output: The result will be as follows. [xml]Current Element :student Student roll no : 393 First Name : dinkar Last Name : kad Nick Name : dinkar Marks : 85 Current Element :student Student roll no : 493 First Name : Vaneet Last Name : Gupta Nick Name : vinni Marks : 95 Current Element :student Student roll no : 593 First Name : jasvir Last Name : singh Nick Name : jazz Marks : 90[/xml]

Summary

shape Key Points

  • XPath uses path expressions to navigate in XML documents
  • XPath will have a library of standard functions
  • XPath is a major element in the XSLT standard