List of usage examples for javax.xml.xpath XPathFactory newInstance
public static XPathFactory newInstance()
Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.
This method is functionally equivalent to:
newInstance(DEFAULT_OBJECT_MODEL_URI)
Since the implementation for the W3C DOM is always available, this method will never fail.
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()); }// ww w .j a v a2 s. c om }
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. ja v a 2 s. c o m*/ } } }
From source file:Main.java
public static void main(String[] args) throws Throwable { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String entryType = null;/* ww w.ja v a2 s . c o m*/ XPathExpression[] expressions = new XPathExpression[] { xpath.compile("local-name(*[local-name() = 'package'])"), xpath.compile("local-name(*[local-name() = 'libapp'])"), xpath.compile("local-name(*[local-name() = 'module'])") }; DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = fac.newDocumentBuilder(); Document doc = parser.parse(args[0]); for (int i = 0; i < expressions.length; i++) { String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING); entryType = mappings.get(found); if (entryType != null && !entryType.trim().isEmpty()) { break; } } System.out.println(entryType); }
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 w w.jav 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 w w w . ja va 2s . c o 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 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 Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/*from w w w . ja v a 2 s . c o m*/ DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("c:\\file.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc)); String query = "//rss/channel/yweather:location/@city"; XPathExpression expr = xPath.compile(query); Object result = expr.evaluate(dDoc, XPathConstants.STRING); System.out.println(result); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new StringReader(xml))); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); Element userElement = (Element) xpath.evaluate("/users/user", document, XPathConstants.NODE); System.out.println(userElement.getAttribute("id")); System.out.println(userElement.getAttribute("firstname")); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);//from w w w. j av a 2s . c o 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()); } }