List of usage examples for javax.xml.xpath XPathConstants NODE
QName NODE
To view the source code for javax.xml.xpath XPathConstants NODE.
Click Source Link
The XPath 1.0 NodeSet data type.
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document retval = dbf.newDocumentBuilder().newDocument(); Element parent = retval.createElement("parent"); retval.appendChild(parent);/* w w w .j av a2 s.c o m*/ Element child1 = retval.createElement("child"); child1.setTextContent("child.text"); parent.appendChild(child1); Element child2 = retval.createElement("child"); child2.setTextContent("child.text.2"); parent.appendChild(child2); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); System.out.println(xPath.evaluate("//child/text()", retval, XPathConstants.NODE).getClass()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder() .parse("http://stackoverflow.com/feeds/tag?tagnames=java&sort=newest"); Element root = doc.getDocumentElement(); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expression = xPath.compile("//entry"); NodeList nl = (NodeList) expression.evaluate(root, XPathConstants.NODESET); System.out.println("Found " + nl.getLength() + " items..."); for (int index = 0; index < nl.getLength(); index++) { Node node = nl.item(index); expression = xPath.compile("title"); Node child = (Node) expression.evaluate(node, XPathConstants.NODE); System.out.println(child.getTextContent()); }//from w w w . j ava 2 s .com }
From source file:MainClass.java
public static void main(String[] args) throws Exception { SimpleDateFormat xmlDateFormat = new SimpleDateFormat("MM.dd.yy"); MapVariableResolver resolver = new MapVariableResolver(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(new File("t.xml")); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); xPath.setXPathVariableResolver(resolver); XPathExpression expression = xPath.compile("/schedule/show[@date=$date]/guest"); String formattedDate = xmlDateFormat.format(new Date(2006, 5, 14)); resolver.addVariable(null, "date", formattedDate); Element guest = (Element) expression.evaluate(document, XPathConstants.NODE); System.out.println(guest.getElementsByTagName("name").item(0).getTextContent()); }
From source file:Main.java
public static void main(final String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . j a v a2s. c om DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(( // "<?xml version=\"1.0\"?>" + // "<people>" + // "<person><name>First Person Name</name></person>" + // "<person><name>Second Person Name</name></person>" + // "</people>" // ).getBytes())); String fragment = "<name>Changed Name</name>"; Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes())); Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild()); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//people/person[2]/name"); Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE); Node parentNode = nodeFound.getParentNode(); parentNode.removeChild(nodeFound); parentNode.appendChild(injectedNode); DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(domSource, result); System.out.println(result.getWriter().toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream( ("<foo><foo1>Foo Test 1</foo1><foo2><another1><test1>Foo Test 2</test1></another1></foo2><foo3>Foo Test 3</foo3><foo4>Foo Test 4</foo4></foo>") .getBytes())); String xpath = "/" + getXPath(document, "test1"); Node node1 = (Node) XPathFactory.newInstance().newXPath().compile(xpath).evaluate(document, XPathConstants.NODE); Node node2 = (Node) XPathFactory.newInstance().newXPath().compile("//test1").evaluate(document, XPathConstants.NODE); System.out.println(node1.equals(node2)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader("<root>\r\n" + // "<Users>\r\n" + // " <App id=\"test\">\r\n" + // " <Username>ADMIN</Username>\r\n" + // " <Password>ADMIN</Password>\r\n" + // " </App>\r\n" + // "</Users>\r\n" + // "<Users>\r\n" + // " <App id=\"test2\">\r\n" + // " <Username>ADMIN2</Username>\r\n" + // " <Password>ADMIN2</Password>\r\n" + // " </App>\r\n" + // "</Users>\r\n" + // "</root>"))); String inputId = "test2"; String xpathStr = "//Users/App[@id='" + inputId + "']"; // retrieve elements and change their content XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile(xpathStr + "/Username"); Node username = (Node) expr.evaluate(doc, XPathConstants.NODE); username.setTextContent("test-username"); expr = xpath.compile(xpathStr + "/Password"); Node password = (Node) expr.evaluate(doc, XPathConstants.NODE); password.setTextContent("test-password"); // output the document Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); System.out.println(writer.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { Main example = new Main(); example.data.put("France", "Paris"); example.data.put("Japan", "Tokyo"); JAXBContext context = JAXBContext.newInstance(Main.class); Marshaller marshaller = context.createMarshaller(); DOMResult result = new DOMResult(); marshaller.marshal(example, result); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Document document = (Document) result.getNode(); XPathExpression expression = xpath.compile("//map/entry"); NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET); expression = xpath.compile("//map"); Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE); Element newMap = document.createElement("map"); for (int index = 0; index < nodes.getLength(); index++) { Element element = (Element) nodes.item(index); newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value")); }//ww w . ja va 2 s . c om expression = xpath.compile("//map/.."); Node parent = (Node) expression.evaluate(document, XPathConstants.NODE); parent.replaceChild(newMap, oldMap); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:ApplyXPathJAXP.java
public static void main(String[] args) { QName returnType = null;/*from www.ja v a 2s . c o 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:Main.java
public static Node getNode(Node node, XPathExpression xpath) { try {/*w w w . ja va2 s . c om*/ Object result = xpath.evaluate(node, XPathConstants.NODE); return (Node) result; } catch (XPathExpressionException e) { throw new RuntimeException("Invalid XPath", e); } }
From source file:Main.java
public static Node selectNode(String xpath, Object node) { return (Node) evaluateXPath(xpath, node, XPathConstants.NODE); }