List of usage examples for javax.xml.xpath XPathExpression evaluate
public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;
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 www . j a v a 2 s.co m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);/*from w w w . jav a 2 s. c o m*/ DocumentBuilder builder = docFactory.newDocumentBuilder(); Document doc = builder.parse("data.xml"); XPathExpression expr = XPathFactory.newInstance().newXPath().compile("/script/data"); Object hits = expr.evaluate(doc, XPathConstants.NODESET); if (hits instanceof NodeList) { NodeList list = (NodeList) hits; for (int i = 0; i < list.getLength(); i++) { System.out.println(list.item(i).getTextContent()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("/data.xml"); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression xPathExpression = xpath.compile("//city/text()"); Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET); System.out.println(result.toString()); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }//from w ww .j a v a2 s . c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { String exp = "/configs/markets/market"; String path = "data.xml"; Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(exp); NodeList nodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET); Document newXmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = newXmlDocument.createElement("root"); newXmlDocument.appendChild(root);//from ww w . j av a 2s . co m for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node copyNode = newXmlDocument.importNode(node, true); root.appendChild(copyNode); } printXmlDocument(newXmlDocument); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = Factory.newDocumentBuilder(); Document doc = builder.parse("myxml.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//" + "item1" + "/*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Element el = (Element) nodes.item(i); System.out.println("tag: " + el.getNodeName()); if (el.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner value:" + el.getFirstChild().getNodeValue()); NodeList children = el.getChildNodes(); for (int k = 0; k < children.getLength(); k++) { Node child = children.item(k); if (child.getNodeType() != Node.TEXT_NODE) { System.out.println("child tag: " + child.getNodeName()); if (child.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner child value:" + child.getFirstChild().getNodeValue()); }/* ww w . j av a 2 s . c o m*/ } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);/*from www. ja va 2 s .co m*/ 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: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 xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); String xml1 = "<xml><product><product_id>2</product_id></product><car><product_id>12345678</product_id></car></xml>"; String xml2 = "<xml><product><product_id>2</product_id></product><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>"; Document doc1 = stringToDom(xml1); Document doc2 = stringToDom(xml2); XPathExpression expr1 = xpath.compile("//car/product_id/text()"); String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING); XPathExpression expr2 = xpath.compile("//car/car_type/text()"); String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING); System.out.println("carId: " + carId); System.out.println("carType: " + carType); }
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(final String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// www . ja v a 2 s . co m 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()); }