Example usage for javax.xml.xpath XPathConstants NUMBER

List of usage examples for javax.xml.xpath XPathConstants NUMBER

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NUMBER.

Prototype

QName NUMBER

To view the source code for javax.xml.xpath XPathConstants NUMBER.

Click Source Link

Document

The XPath 1.0 number data type.

Maps to Java Double .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xp = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new FileInputStream("input.xml"));
    double count = (Double) xp.evaluate("count(//slot[@name='slot1']/port)", xml, XPathConstants.NUMBER);
}

From source file:GetSeriesId.java

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

    Double result = (Double) xPath.evaluate("/schedule/@seriesId", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println(result.intValue());
}

From source file:GuestListCounter.java

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

    Number shows = (Number) xPath.evaluate("count(/schedule/show)", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println("Document has " + shows.intValue() + " shows.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);//w w  w  .jav a  2 s.com
    DocumentBuilder builder;
    builder = docFactory.newDocumentBuilder();
    Document doc = builder.parse(CFG_FILE);
    XPathExpression expr = XPathFactory.newInstance().newXPath().compile(XPATH_FOR_PRM_MaxThread);
    Object result = expr.evaluate(doc, XPathConstants.NUMBER);
    if (result instanceof Double) {
        System.out.println(((Double) result).intValue());
    }

}

From source file:ApplyXPathJAXP.java

public static void main(String[] args) {
    QName returnType = null;/* ww  w .j av a 2s .co m*/

    if (args.length != 3) {
        System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type");
    }

    InputSource xml = new InputSource(args[0]);
    String expr = args[1];

    // set the return type
    if (args[2].equals("num"))
        returnType = XPathConstants.NUMBER;
    else if (args[2].equals("bool"))
        returnType = XPathConstants.BOOLEAN;
    else if (args[2].equals("str"))
        returnType = XPathConstants.STRING;
    else if (args[2].equals("node"))
        returnType = XPathConstants.NODE;
    else if (args[2].equals("nodeset"))
        returnType = XPathConstants.NODESET;
    else
        System.err.println("Invalid return type: " + args[2]);

    // Create a new XPath
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Object result = null;
    try {
        // compile the XPath expression
        XPathExpression xpathExpr = xpath.compile(expr);

        // Evaluate the XPath expression against the input document
        result = xpathExpr.evaluate(xml, returnType);

        // Print the result to System.out.
        printResult(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ExtensionFunctionResolver.java

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

    // set the NamespaceContext to 
    // org.apache.xalan.extensions.ExtensionNamespaceContext
    xpath.setNamespaceContext(new ExtensionNamespaceContext());

    // set the XPathFunctionResolver to 
    // org.apache.xalan.extensions.XPathFunctionResolverImpl
    xpath.setXPathFunctionResolver(new XPathFunctionResolverImpl());

    Object result = null;/*from  ww w.j ava 2s  . c  o m*/
    // Evaluate the XPath expression "math:max(/doc/num)" against 
    // the input document numlist.xml
    InputSource context = new InputSource("numlist.xml");
    result = xpath.evaluate(EXPR1, context, XPathConstants.NUMBER);
    System.out.println(EXPR1 + " = " + result);

    // Evaluate the XPath expression "java:ExtensionTest.test('Bob')"
    result = xpath.evaluate(EXPR2, context, XPathConstants.STRING);
    System.out.println(EXPR2 + " = " + result);
}

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   w ww. j  a v  a 2s.co  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:XPathResolver.java

public static void main(String[] args) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    // Set the NamespaceContext
    xpath.setNamespaceContext(new MyNamespaceContext());
    // Set the function resolver
    xpath.setXPathFunctionResolver(new MyFunctionResolver());
    // Set the variable resolver
    xpath.setXPathVariableResolver(new MyVariableResolver());

    Object result = null;//from   w w  w . j  a  v  a2  s  . c om
    try {
        result = xpath.evaluate(EXPR, (Object) null, XPathConstants.NUMBER);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // The evaluation result is 9.0.
    System.out.println("The evaluation result: " + result);
}

From source file:Main.java

public static Number selectNumber(Document xmlDocument, String expression) throws XPathExpressionException {
    return (Number) selectObject(xmlDocument, expression, XPathConstants.NUMBER);
}

From source file:Main.java

public static int getInt(Object node, XPathExpression expression) throws XPathExpressionException {
    return ((Double) expression.evaluate(node, XPathConstants.NUMBER)).intValue();
}