Example usage for javax.xml.xpath XPath evaluate

List of usage examples for javax.xml.xpath XPath evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPath evaluate.

Prototype

public String evaluate(String expression, InputSource source) throws XPathExpressionException;

Source Link

Document

Evaluate an XPath expression in the context of the specified InputSource and return the result as a String .

Usage

From source file:GetName.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    String result = xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")));
    System.out.println(result);/*from   w w w.  j av  a 2  s .  c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputSource inputSource = new InputSource("input.xml");
    XPath xPath = XPathFactory.newInstance().newXPath();
    String text = xPath.evaluate("/note/body", inputSource);
    System.out.println(text);//  w w w  .  ja  v  a  2  s.  c  o  m
}

From source file:MainClass.java

public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException,
        org.xml.sax.SAXException, java.io.IOException {
    String documentName = args[0];
    String expression = args[1];//from ww  w  .j  a  v  a 2  s  .c  o  m

    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = parser.parse(new java.io.File(documentName));

    XPath xpath = XPathFactory.newInstance().newXPath();

    System.out.println(xpath.evaluate(expression, doc));

    NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    for (int i = 0, n = nodes.getLength(); i < n; i++) {
        Node node = nodes.item(i);
        System.out.println(node);
    }
}

From source file:Main.java

public static void main(String[] args) {

    String simpleXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Bob</name></person>";
    InputSource inputSource = new InputSource(new StringReader(simpleXML));
    XPath xpath = XPathFactory.newInstance().newXPath();

    try {/* w w w  . j  av a  2  s.  co m*/
        String name = xpath.evaluate("//name", inputSource);
        System.out.println(name);
    } catch (XPathExpressionException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("file:////tmp/whatever.xml");
    String version = xpath.evaluate("//behavior/@version", doc);
    System.out.println(version);/* w w  w.  jav  a 2s. c  om*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
    nsContext.addNamespace("s", "uri:test:schedule");
    xPath.setNamespaceContext(nsContext);

    String result = xPath.evaluate("/s:schedule/@name", new InputSource(new FileReader("t.xml")));
    System.out.println(result);//from   w  w  w . ja  v  a 2s  .c  o  m
}

From source file:android.databinding.tool.MakeCopy.java

public static void main(String[] args) {
    if (args.length < 5) {
        System.out.println("required parameters: [-l] manifest adk-dir src-out-dir xml-out-dir "
                + "res-out-dir res-in-dir...");
        System.out.println("Creates an android data binding class and copies resources from");
        System.out.println("res-source to res-target and modifies binding layout files");
        System.out.println("in res-target. Binding data is extracted into XML files");
        System.out.println("and placed in xml-out-dir.");
        System.out.println("  -l          indicates that this is a library");
        System.out.println("  manifest    path to AndroidManifest.xml file");
        System.out.println("  src-out-dir path to where generated source goes");
        System.out.println("  xml-out-dir path to where generated binding XML goes");
        System.out.println("  res-out-dir path to the where modified resources should go");
        System.out.println(/*from  ww  w  .  j  a v a 2 s.  c  o m*/
                "  res-in-dir  path to source resources \"res\" directory. One" + " or more are allowed.");
        System.exit(1);
    }
    final boolean isLibrary = args[0].equals("-l");
    final int indexOffset = isLibrary ? 1 : 0;
    final String applicationPackage;
    final int minSdk;
    final Document androidManifest = readAndroidManifest(new File(args[MANIFEST_INDEX + indexOffset]));
    try {
        final XPathFactory xPathFactory = XPathFactory.newInstance();
        final XPath xPath = xPathFactory.newXPath();
        applicationPackage = xPath.evaluate("string(/manifest/@package)", androidManifest);
        final Double minSdkNumber = (Double) xPath.evaluate("number(/manifest/uses-sdk/@android:minSdkVersion)",
                androidManifest, XPathConstants.NUMBER);
        minSdk = minSdkNumber == null ? 1 : minSdkNumber.intValue();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        System.exit(6);
        return;
    }
    final File srcDir = new File(args[SRC_INDEX + indexOffset], APP_SUBPATH);
    if (!makeTargetDir(srcDir)) {
        System.err.println("Could not create source directory " + srcDir);
        System.exit(2);
    }
    final File resTarget = new File(args[RES_OUT_INDEX + indexOffset]);
    if (!makeTargetDir(resTarget)) {
        System.err.println("Could not create resource directory: " + resTarget);
        System.exit(4);
    }
    final File xmlDir = new File(args[XML_INDEX + indexOffset]);
    if (!makeTargetDir(xmlDir)) {
        System.err.println("Could not create xml output directory: " + xmlDir);
        System.exit(5);
    }
    System.out.println("Application Package: " + applicationPackage);
    System.out.println("Minimum SDK: " + minSdk);
    System.out.println("Target Resources: " + resTarget.getAbsolutePath());
    System.out.println("Target Source Dir: " + srcDir.getAbsolutePath());
    System.out.println("Target XML Dir: " + xmlDir.getAbsolutePath());
    System.out.println("Library? " + isLibrary);

    boolean foundSomeResources = false;
    for (int i = RES_IN_INDEX + indexOffset; i < args.length; i++) {
        final File resDir = new File(args[i]);
        if (!resDir.exists()) {
            System.out.println("Could not find resource directory: " + resDir);
        } else {
            System.out.println("Source Resources: " + resDir.getAbsolutePath());
            try {
                FileUtils.copyDirectory(resDir, resTarget);
                addFromFile(resDir, resTarget);
                foundSomeResources = true;
            } catch (IOException e) {
                System.err.println("Could not copy resources from " + resDir + " to " + resTarget + ": "
                        + e.getLocalizedMessage());
                System.exit(3);
            }
        }
    }

    if (!foundSomeResources) {
        System.err.println("No resource directories were found.");
        System.exit(7);
    }
    processLayoutFiles(applicationPackage, resTarget, srcDir, xmlDir, minSdk, isLibrary);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>";

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    NamespaceContext context = new NamespaceContext() {
        String PREFIX = "nfe";
        String URI = "http://www.java2s.com/nfe";

        @Override//from w w  w.  j  a  va 2  s  .  c  o m
        public String getNamespaceURI(String prefix) {
            return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI;
        }

        @Override
        public String getPrefix(String namespaceUri) {
            return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX;
        }

        @Override
        public Iterator getPrefixes(String namespaceUri) {
            return Collections.singletonList(this.getPrefix(namespaceUri)).iterator();
        }
    };
    xPath.setNamespaceContext(context);
    InputSource inputSource = new InputSource(new StringReader(source));
    String versao = xPath.evaluate("//nfe:p/@versao", inputSource);
    System.out.println(versao.toString());
}

From source file:Main.java

public static String getQuery(String id) throws Exception {
    InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    InputSource inputSource = new InputSource(is);
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.evaluate("/queries/query[@id='" + id + "']", inputSource);
}

From source file:Main.java

public static String stringValue(Object item) throws Exception {
    XPath xpath = factory.newXPath();
    return xpath.evaluate("text()", item);
}