List of usage examples for javax.xml.xpath XPathConstants NODESET
QName NODESET
To view the source code for javax.xml.xpath XPathConstants NODESET.
Click Source Link
The XPath 1.0 NodeSet data type.
Maps to Java org.w3c.dom.NodeList .
From source file:Main.java
public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent("new value"); }//from w ww . j a v a 2s. co m Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml"))); }
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 a v a2 s . co m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// w w w.j a v a 2 s .c o m 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 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()); }/*from ww w . j av a 2 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 w w w .j a va2 s.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(); 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()); }/* ww w. j a va 2 s . c om*/ }
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()); }//www. j a va 2 s . c o m }
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 w ww. j a v a 2 s . c om node.setNodeValue("42"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xPath = XPathFactory.newInstance().newXPath(); FileInputStream file = new FileInputStream(new File("c:/data.xml")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(file); XPathExpression expr = xPath.compile("//project/*"); NodeList list = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); System.out.println(node.getNodeName() + "=" + node.getTextContent()); }//from w ww. j a v a 2s.c o m }
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("Table.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath path = xFactory.newXPath(); XPathExpression exp = path.compile("/tables/table"); NodeList nlTables = (NodeList) exp.evaluate(doc, XPathConstants.NODESET); for (int tblIndex = 0; tblIndex < nlTables.getLength(); tblIndex++) { Node table = nlTables.item(tblIndex); Node nAtt = table.getAttributes().getNamedItem("title"); System.out.println(nAtt.getTextContent()); exp = path.compile("headings/heading"); NodeList nlHeaders = (NodeList) exp.evaluate(table, XPathConstants.NODESET); Set<String> headers = new HashSet<String>(25); for (int index = 0; index < nlHeaders.getLength(); index++) { headers.add(nlHeaders.item(index).getTextContent().trim()); }// w ww.ja va2s. c om for (String header : headers) { System.out.println(header); } exp = path.compile("tablebody/tablerow"); NodeList nlRows = (NodeList) exp.evaluate(table, XPathConstants.NODESET); for (int index = 0; index < nlRows.getLength(); index++) { Node rowNode = nlRows.item(index); exp = path.compile("tablecell/item"); NodeList nlValues = (NodeList) exp.evaluate(rowNode, XPathConstants.NODESET); List<String> values = new ArrayList<String>(25); for (int valueIndex = 0; valueIndex < nlValues.getLength(); valueIndex++) { values.add(nlValues.item(valueIndex).getTextContent().trim()); } for (String value : values) { System.out.println(value); } } } }