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:MainClass.java
public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, org.xml.sax.SAXException, java.io.IOException { String documentName = args[0]; String expression = args[1];/*from w w w . java2s. c o m*/ DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = parser.parse(new java.io.File(documentName)); XPath xpath = XPathFactory.newInstance().newXPath(); System.out.println(xpath.evaluate(expression, doc)); NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); for (int i = 0, n = nodes.getLength(); i < n; i++) { Node node = nodes.item(i); System.out.println(node); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<metadata><codes class = 'class1'>" + "<code code='ABC'>" + "<detail x='blah blah'/>" + "</code>" + "</codes>" + "<codes class = 'class2'>" + "<code code = '123'>" + "<detail x='blah blah'/></code></codes></metadata>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes())); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength() > 0 ? "Yes" : "No"); for (int i = 0; i < nodes.getLength(); i++) { System.out.println("nodes: " + nodes.item(i).getNodeValue()); }//w ww .j a va2 s . c o m }
From source file:Main.java
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/*from w ww . j a v a 2s .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 { 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);/*from w w w . j a v a 2s .c om*/ 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); } }
From source file:Main.java
public static void main(String argv[]) throws Exception { String next = "keyword,123"; String[] input = next.split(","); String textToFind = input[0].replace("'", "\\'"); // "CEO"; String textToReplace = input[1].replace("'", "\\'"); // "Chief Executive Officer"; String filepath = "root.xml"; String fileToBeSaved = "root2.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); XPath xpath = XPathFactory.newInstance().newXPath(); // change ELEMENTS String xPathExpression = "//*[text()='" + textToFind + "']"; NodeList nodes = (NodeList) xpath.evaluate(xPathExpression, doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent(textToReplace); }/*from w w w . j a va 2s .c o m*/ // change ATTRIBUTES String xPathExpressionAttr = "//*/@*[.='" + textToFind + "']"; NodeList nodesAttr = (NodeList) xpath.evaluate(xPathExpressionAttr, doc, XPathConstants.NODESET); for (int i = 0; i < nodesAttr.getLength(); i++) { nodesAttr.item(i).setTextContent(textToReplace); } System.out.println("Everything replaced."); // save xml file back TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(fileToBeSaved)); transformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<root>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.75</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "</ctc:BasePrice>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.25</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "<cgc:MinimumQuantity quantityUnitCode='EA'>3</cgc:MinimumQuantity>" + "</ctc:BasePrice>" + "</root>"; InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(xmlStream); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "//*[local-name() = 'BasePrice' and not(descendant::*[local-name() = 'MinimumQuantity'])]/*[local-name()='PriceAmount']"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); }/*from w w w. j a v a 2 s. co m*/ }
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")); }//w w w . ja v a 2s .c o m 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:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false);// w w w. j av a 2 s . com domFactory.setNamespaceAware(true); domFactory.setIgnoringComments(true); domFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("C:/data.xsd"); Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0); System.out.println(rootNode.getNodeName()); XPath xPath1 = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { return "http://www.w3.org/2001/XMLSchema"; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { Set s = new HashSet(); s.add("xs"); return s.iterator(); } }; xPath1.setNamespaceContext((NamespaceContext) nsContext); NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET); System.out.println(nList1.item(0).getNodeName()); NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET); System.out.println(nList2.item(0).getNodeName()); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { String xml = "<soapenv:Envelope " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header>" + "<authInfo xsi:type=\"soap:authentication\" " + "xmlns:soap=\"http://list.com/services/SoapRequestProcessor\">" + "<username xsi:type=\"xsd:string\">asdf@g.com</username>" + "<password xsi:type=\"xsd:string\">asdf</password></authInfo></soapenv:Header></soapenv:Envelope>"; System.out.println(xml);//from www .j a v a2 s .c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator getPrefixes(String arg0) { return null; } @Override public String getPrefix(String arg0) { return null; } @Override public String getNamespaceURI(String arg0) { if ("soapenv".equals(arg0)) { return "http://schemas.xmlsoap.org/soap/envelope/"; } return null; } }); XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println("Got " + nodes.getLength() + " nodes"); }
From source file:ApplyXPathJAXP.java
public static void main(String[] args) { QName returnType = null;/*from w w w.j a v a 2s. co 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(); } }