How To Practise Too Evaluate Xpath Seem Inward Coffee - Tutorial Too Example

You tin utilisation XPathExpression from javax.xml.xpath parcel to create too execute XPATH human face inward Java.  Java API provides javax.xml.xpath  package, which contains classes similar Xpath, XpathFactory to piece of job amongst XPATH too XML documents. By the agency this is tertiary article on Xpath, In concluding yoke of tutorials e.g. XPATH examples to pick out values using attributes too my XPATH notes for Java programmer, nosotros bring seen basics of Xpath human face from XML too Java signal of view.  Anyone who has piece of job amongst XML technologies inward Java knows importance of XPATH, it is i the almost useful applied scientific discipline to recall selected information from XML files. XPATH is similar SQL which is used to recall information from relational database. Many back-office too middle-office Java application which transfers information inward cast of XML documents makes extensive utilisation of  XPATH human face to recall value for printing or for determination making. Though at that topographic point are lot of opened upward root XML libraries are available to manage amongst Java too XML evolution e.g. XML Beans. I personally prefer to utilisation measure Java API if  functionality is available there. In this Java tutorial nosotros volition acquire how to create XPath human face inward Java programme too retrieving values from XPATH e.g. text, numbers too boolean values, which is critical for programming flow.

Java XPATH human face Example

xpath parcel to create too execute XPATH human face inward Java How to Create too Evaluate XPath Expression inward Java - Tutorial too ExampleCreating XPATH human face is simple, only navigate to the node you lot wishing to recall past times putting "/" similar inward our instance if nosotros wishing to recall models of each smartphone than nosotros volition write Xpath human face every bit /smartphones/smartphone/model. Apart from navigating to desired node inward Xpath human face nosotros bring too used utility method text() to recall text too count() to count expose of matching results. Java classes from java.xml.xpath parcel e.g. XPath, XPathFactory too XPathExpression is used to create too evaluate Xpath inward Java programming language.



/**
 * Java programme to demonstrate How to create XPATH expression, How to execute XPATH
 * human face to recall text value, numeric value too 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 MD = 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 human face for getting all brands
        XPathExpression xPathExpr = xpath.compile("/smartphones/smartphone/brand/text()");
     
        // XPath text instance : executing xpath human face inward java
        Object effect = 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 human face inward java
        xPathExpr = xpath.compile("/smartphones/smartphone/model/text()");
        effect = xPathExpr.evaluate(doc, XPathConstants.NODESET);
        System.out.println("Java Xpath text example: All pop smartphone model ");
        printXpathResult(result);
     
        // XPath count instance : XPath human face 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 bring whatsoever HTC smartphone
        xPathExpr = xpath.compile("count(/smartphones/smartphone[brand='HTC']) > 0");
        Boolean examine = (Boolean) xPathExpr.evaluate(doc, XPathConstants.BOOLEAN);
        System.out.println("XPath boolean example: Do nosotros bring whatsoever 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 bring whatsoever HTC smartphone
False


That’s all on How to create too evaluate XPath human face inward Java . We bring seen How to pick out XML elements value every bit text using text() business office too counted matching elements using count() function. Xpath is existent interesting materials too i time you lot acquire the basics, its real slow to piece of job amongst Xpath both inward XML editors too Java programming language.

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

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Practise Too Evaluate Xpath Seem Inward Coffee - Tutorial Too Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel