List of usage examples for javax.xml.xpath XPath compile
public XPathExpression compile(String expression) throws XPathExpressionException;
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 w w w.ja v a2s .co m }
From source file:Main.java
public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Test.xml")); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression exp = xPath.compile("//data"); NodeList nl = (NodeList) exp.evaluate(doc, XPathConstants.NODESET); System.out.println("Found " + nl.getLength() + " results"); }
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);/* w w w. j a va2 s .c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// w w w .j av a2s .com DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("test.xml"); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression expr = xpath.compile("//element[@key='property1']/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } }
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 { 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 ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/*from ww w.jav a 2 s . c o m*/ DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//numDocs"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } }
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(new File("path/to/file.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()"); NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getParentNode().getNodeName().equals("Name")) { node.setNodeValue("new name"); } else {/*from ww w . j av a 2 s. co m*/ node.setNodeValue("42"); } } }
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()); }/* w ww . j av a 2 s .c o m*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { List<String> names = new ArrayList<>(); URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265"); InputStream is = oracle.openStream(); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);// www . j av a2 s . co m DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(is); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//*:*/@*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nl = (NodeList) result; for (int i = 0; i < nl.getLength(); i++) { names.add(nl.item(i).getNodeName()); Node node = nl.item(i); String path = "." + node.getNodeName() + " = " + node.getNodeValue(); node = ((Attr) node).getOwnerElement(); while (node != null) { path = node.getNodeName() + '/' + path; node = node.getParentNode(); } System.out.println(path); } }