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 { XPathFactory xpf = XPathFactory.newInstance(); XPath xp = xpf.newXPath();//from ww w . j a v a2 s .co m xp.setNamespaceContext(new MyNamespaceContext()); XPathExpression xpe = xp.compile("ns:feed/ns:entry"); FileInputStream xmlStream = new FileInputStream("input.xml"); InputSource xmlInput = new InputSource(xmlStream); Element result = (Element) xpe.evaluate(xmlInput, XPathConstants.NODE); System.out.println(result); }
From source file:Main.java
public static void main(String[] args) { String simpleXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Bob</name></person>"; InputSource inputSource = new InputSource(new StringReader(simpleXML)); XPath xpath = XPathFactory.newInstance().newXPath(); try {//from w w w .j a v a2s . co m String name = xpath.evaluate("//name", inputSource); System.out.println(name); } catch (XPathExpressionException e) { System.out.println(e.getMessage()); } }
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);//from w w w .ja v a 2 s.c om }
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 { 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 w w .j a v a 2s . c om }
From source file:Main.java
public static void main(String[] args) throws Exception { String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>"; XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); NamespaceContext context = new NamespaceContext() { String PREFIX = "nfe"; String URI = "http://www.java2s.com/nfe"; @Override//from w w w.j a v a2 s . c om public String getNamespaceURI(String prefix) { return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI; } @Override public String getPrefix(String namespaceUri) { return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX; } @Override public Iterator getPrefixes(String namespaceUri) { return Collections.singletonList(this.getPrefix(namespaceUri)).iterator(); } }; xPath.setNamespaceContext(context); InputSource inputSource = new InputSource(new StringReader(source)); String versao = xPath.evaluate("//nfe:p/@versao", inputSource); System.out.println(versao.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xml = db.parse("input.xml"); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); String health = (String) xpath.evaluate("/game/health", xml, XPathConstants.STRING); System.out.println(health);/*from w ww. jav a 2 s . co m*/ }
From source file:MainClass.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.addNamespace("s", "uri:test:schedule"); xPath.setNamespaceContext(nsContext); String result = xPath.evaluate("/s:schedule/@name", new InputSource(new FileReader("t.xml"))); System.out.println(result);//from w w w. j a va 2s.c om }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("input.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); String string = (String) xPath.evaluate("/documents/document/element[@name='doctype']/value", dDoc, XPathConstants.STRING); System.out.println(string);/*from w w w . j av a 2 s. co m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); InputSource xml = new InputSource(new StringReader(XML)); String result = (String) xpath.evaluate("//@Build-Label", xml, XPathConstants.STRING); System.out.println(result);/*from w w w . j a v a2s . c om*/ }