How To Practice Together With Evaluate Xpath Appear Inwards Coffee - Tutorial Together With Example

You tin usage XPathExpression from javax.xml.xpath packet to create as well as execute XPATH facial expression inward Java.  Java API provides javax.xml.xpath  package, which contains classes similar Xpath, XpathFactory to piece of employment alongside XPATH as well as XML documents. By the agency this is tertiary article on Xpath, In final span of tutorials e.g. XPATH examples to pick out values using attributes as well as my XPATH notes for Java programmer, nosotros accept seen basics of Xpath facial expression from XML as well as Java quest of view.  Anyone who has piece of employment alongside XML technologies inward Java knows importance of XPATH, it is 1 the almost useful technology scientific discipline to cry back selected information from XML files. XPATH is similar SQL which is used to cry back information from relational database. Many back-office as well as middle-office Java application which transfers information inward shape of XML documents makes extensive usage of  XPATH facial expression to cry back value for printing or for conclusion making. Though at that spot are lot of opened upward beginning XML libraries are available to assistance alongside Java as well as XML evolution e.g. XML Beans. I personally prefer to usage measure Java API if  functionality is available there. In this Java tutorial nosotros volition larn how to create XPath facial expression inward Java computer program as well as retrieving values from XPATH e.g. text, numbers as well as boolean values, which is critical for programming flow.

Java XPATH facial expression Example

xpath packet to create as well as execute XPATH facial expression inward Java How to Create as well as Evaluate XPath Expression inward Java - Tutorial as well as ExampleCreating XPATH facial expression is simple, only navigate to the node you lot wishing to cry back past times putting "/" similar inward our event if nosotros wishing to cry back models of each smartphone than nosotros volition write Xpath facial expression equally /smartphones/smartphone/model. Apart from navigating to desired node inward Xpath facial expression nosotros accept too used utility method text() to cry back text as well as count() to count seat out of matching results. Java classes from java.xml.xpath packet e.g. XPath, XPathFactory as well as XPathExpression is used to create as well as evaluate Xpath inward Java programming language.



/**
 * Java computer program to demonstrate How to create XPATH expression, How to execute XPATH
 * facial expression to cry back text value, numeric value as well as boolean value.
 * 
 */
package test;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XPathExample {

    public void xPathProcessor() throws SAXException, IOException
                  ,  XPathExpressionException, ParserConfigurationException {

     
        //Create DocumentBuilderFactory for reading xml file
        DocumentBuilderFactory manufacturing flora = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();;
        Document Dr. = builder.parse("smartphone.xml");
     
        // Create XPathFactory for creating XPath Object
        XPathFactory xPathFactory = XPathFactory.newInstance();

        // Create XPath object from XPathFactory
        XPath xpath = xPathFactory.newXPath();

        // Compile the XPath facial expression for getting all brands
        XPathExpression xPathExpr = xpath.compile("/smartphones/smartphone/brand/text()");
     
        // XPath text event : executing xpath facial expression inward java
        Object outcome = xPathExpr.evaluate(doc, XPathConstants.NODESET);
        System.out.println("Java Xpath text example: All brands of pop smartphones ");
        printXpathResult(result);

        //get all models past times xpath facial expression inward java
        xPathExpr = xpath.compile("/smartphones/smartphone/model/text()");
        outcome = xPathExpr.evaluate(doc, XPathConstants.NODESET);
        System.out.println("Java Xpath text example: All pop smartphone model ");
        printXpathResult(result);
     
        // XPath count event : XPath facial expression to count elements inward xml
        xPathExpr = xpath.compile("count(/smartphones/smartphone)");
        Double count = (Double) xPathExpr.evaluate(doc, XPathConstants.NUMBER);
        System.out.println("XPath count example: How many Smartphones nosotros have: ");
        System.out.println("Count of elements: " + count);

     
        // XPath conditional exampl e: Do nosotros accept whatever HTC smartphone
        xPathExpr = xpath.compile("count(/smartphones/smartphone[brand='HTC']) > 0");
        Boolean seek = (Boolean) xPathExpr.evaluate(doc, XPathConstants.BOOLEAN);
        System.out.println("XPath boolean example: Do nosotros accept whatever HTC smartphone ");
        System.out.println(test);

    }
 
    public void printXpathResult(Object result){
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
        }
    }

    public static void main(String[] args) throws XpathExpressionException
               ,  ParserConfigurationException, SAXException, IOException {

        XPathExample xPathExample = new XPathExample();
        xPathExample.xPathProcessor();
    }
}

Output:
Java Xpath text example: All brands of pop smartphones
Apple
Samsung
Nokia

Java Xpath text example: All pop smartphone model
IPhone4S, IPhone4, IPhone5
Milky Way S2, Milky Way nexus, Milky Way Ace
Lumia 800, Lumia 710, Nokia N9
XPath count example: How many Smartphones nosotros have:
Count of elements: 3.0
XPath boolean example: Do nosotros accept whatever HTC smartphone
False


That’s all on How to create as well as evaluate XPath facial expression inward Java . We accept seen How to pick out XML elements value equally text using text() role as well as counted matching elements using count() function. Xpath is existent interesting materials as well as 1 time you lot acquire the basics, its rattling slow to piece of employment alongside Xpath both inward XML editors as well as Java programming language.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services as well as REST API alongside Spring Boot
Difference betwixt SAX as well as DOM parser inward Java

Belum ada Komentar untuk "How To Practice Together With Evaluate Xpath Appear Inwards Coffee - Tutorial Together With Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel