Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new InputSource(new StringReader(xml)));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    Element userElement = (Element) xpath.evaluate("/users/user", document, XPathConstants.NODE);
    System.out.println(userElement.getAttribute("id"));
    System.out.println(userElement.getAttribute("firstname"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("filename.xml"));

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xmlfile = "data.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(xmlfile);

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("/root/param[@name='name2']", doc, XPathConstants.NODE);
    System.out.println(element.getTextContent());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile(
            "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");
    NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getTextContent());
    }/*from ww w  .  j  a va2 s  .c om*/
}

From source file:Main.java

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

    NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODESET);
    for (int i = 0; i < shows.getLength(); i++) {
        Element show = (Element) shows.item(i);
        String guestName = xPath.evaluate("guest/name", show);
        System.out.println(guestName);
        System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date"));
    }/*from w  w  w .j  a  v  a 2 s .c  om*/

}

From source file:Main.java

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

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expression = xpath.compile("//data/user/username[text()='simple']");
    Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
    b13Node = b13Node.getParentNode();
    b13Node.getParentNode().removeChild(b13Node);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    xp.setNamespaceContext(new MyNamespaceContext());
    XPathExpression xpe = xp.compile("ns:feed/ns:entry");
    FileInputStream xmlStream = new FileInputStream("input.xml");
    InputSource xmlInput = new InputSource(xmlStream);
    Element result = (Element) xpe.evaluate(xmlInput, XPathConstants.NODE);
    System.out.println(result);/*from www  . ja v a2 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());
        }//from   w  w  w .j a va 2s .  c om
    }
}

From source file:Main.java

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

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]");

    Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
    b13Node.getParentNode().removeChild(b13Node);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:GuestList.java

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

    NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODESET);
    for (int i = 0; i < shows.getLength(); i++) {
        Element show = (Element) shows.item(i);
        String guestName = xPath.evaluate("guest/name", show);
        String guestCredit = xPath.evaluate("guest/credit", show);

        System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName
                + " (" + guestCredit + ")");
    }/*from  w  w w.jav  a  2 s. co  m*/

}