convenience method to generate an XPathExpression object from a string representing an Xpath - Android XML

Android examples for XML:XPath

Description

convenience method to generate an XPathExpression object from a string representing an Xpath

Demo Code


//package com.java2s;
import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    public static void main(String[] argv) throws Exception {
        String pathstring = "java2s.com";
        System.out.println(makeXPathFromString(pathstring));
    }//from  w  w w.  ja  v a2  s  . c  o m

    /**
     * convenience method to generate an XPathExpression object from a string representing an Xpath
     * @param pathstring the string
     * @return an Xpath object, or null if there was an exception
     */
    public static XPathExpression makeXPathFromString(String pathstring) {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = null;
        try {
            expr = xpath.compile(pathstring);
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return expr;
    }
}

Related Tutorials