Example usage for javax.xml.xpath XPathConstants STRING

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

Introduction

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

Prototype

QName STRING

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

Click Source Link

Document

The XPath 1.0 string data type.

Maps to Java String .

Usage

From source file:Main.java

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

    InputSource xml = new InputSource("input.xml");
    String health = (String) xpath.evaluate("/Game/health", xml, XPathConstants.STRING);
    System.out.println(health);// w  w  w  .j  a  va  2  s.c  om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String docroot = "<div><i>items <b>sold</b></i></div>";
    XPath xxpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(docroot));
    String result = (String) xxpath.evaluate("//b", inputSource, XPathConstants.STRING);
    System.out.println(result);//from w  w  w  .  j  a v  a  2  s. co  m
}

From source file:Main.java

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

    InputSource inputSource = new InputSource(new StringReader(XML));
    String result = (String) xPath.evaluate("//gender", inputSource, XPathConstants.STRING);
    System.out.println(result);/*from  w w  w  . ja  va  2 s .c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream inputStream = new FileInputStream("data.xml");
    InputSource inputSource = new InputSource(inputStream);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("/PaymentElement/Payment/@seqID");
    Object result = expr.evaluate(inputSource, XPathConstants.STRING);
    System.out.println(result);/*from  ww w.  j  a va2s . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document xml = db.parse("input.xml");

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    String health = (String) xpath.evaluate("/game/health", xml, XPathConstants.STRING);
    System.out.println(health);/* w ww .j  av  a 2  s. co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("input.xml");

    XPath xPath = XPathFactory.newInstance().newXPath();
    String string = (String) xPath.evaluate("/documents/document/element[@name='doctype']/value", dDoc,
            XPathConstants.STRING);
    System.out.println(string);//from  w ww. j  av  a2 s . co m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new StringReader(XML));
    String result = (String) xpath.evaluate("//@Build-Label", xml, XPathConstants.STRING);
    System.out.println(result);/* w w  w .  j a va2  s.  c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    XPathExpression schoolNameExpression = xPath.compile("SchoolName");
    XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName");

    InputSource inputSource = new InputSource("input.xml");
    NodeList schoolNodes = (NodeList) xPath.evaluate("/Data/Schools/School", inputSource,
            XPathConstants.NODESET);
    for (int x = 0; x < schoolNodes.getLength(); x++) {
        Node schoolElement = schoolNodes.item(x);
        System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING));
        NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET);
        for (int y = 0; y < classNames.getLength(); y++) {
            System.out.println(classNames.item(y).getTextContent());
        }/*  www .  java 2s. com*/
    }
}

From source file:Main.java

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

    InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml"))));

    String expression = "//Home/data";
    XPathExpression xPathExpression = xPath.compile(expression);

    NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
    xPathExpression = xPath.compile("@type");

    for (int i = 0; i < elem1List.getLength(); i++) {
        System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING));
    }/*from  w ww . j  a v a  2  s  . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*  w  w w  .j  a va2  s  . com*/
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("c:\\file.xml");
    XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc));
    String query = "//rss/channel/yweather:location/@city";
    XPathExpression expr = xPath.compile(query);
    Object result = expr.evaluate(dDoc, XPathConstants.STRING);
    System.out.println(result);
}