Example usage for javax.xml.xpath XPathConstants NODE

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

Introduction

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

Prototype

QName NODE

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

Click Source Link

Document

The XPath 1.0 NodeSet data type.

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(new FileReader("input.xml"));
    Node result = (Node) xpath.evaluate("/tag1", xml, XPathConstants.NODE);
    System.out.println(result);//  w  w w .j ava2 s.c o  m
}

From source file:GetNameAsAttr.java

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

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

}

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 docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("data.xml"));

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//node3/name", document, XPathConstants.NODE);
}

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 xml = "<company><year id='2000'><quarter id='1' sales='80'/></year><year id='2001'><quarter id='1' sales='20'/></year></company>";
    String xpath = "/company/year[@id=2001]";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.evaluate(xpath, new InputSource(new StringReader(xml)), XPathConstants.NODE);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(node), 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();//from  w ww .  jav  a2 s.  c  o m
    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 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 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: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));
}