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 xpath = xpf.newXPath(); InputSource xml = new InputSource(new FileReader("input.xml")); Node result = (Node) xpath.evaluate("/tag1", xml, XPathConstants.NODE); System.out.println(result);/* ww w . jav a 2 s .c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xPath = XPathFactory.newInstance().newXPath(); FileReader reader = new FileReader("input.xml"); InputSource xml = new InputSource(reader); NodeList titleNodes = (NodeList) xPath.evaluate("//item/title", xml, XPathConstants.NODESET); for (int x = 0; x < titleNodes.getLength(); x++) { System.out.println(titleNodes.item(x).getTextContent()); }/* w ww . jav a 2s .c o m*/ }
From source file:GetNameAsAttr.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")), XPathConstants.NODE); System.out.println(result.getValue()); result.setValue("The Colbert Report"); }
From source file:Main.java
public static void main(String[] args) throws Exception { String docroot = "<div><i>items <b>sold</b></i></div>"; XPath xxpath = XPathFactory.newInstance().newXPath(); InputSource inputSource = new InputSource(new StringReader(docroot)); String result = (String) xxpath.evaluate("//b", inputSource, XPathConstants.STRING); System.out.println(result);/*from ww w . j a v a 2s . c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<car><manufacturer>toyota</manufacturer></car>"; String xpath = "/car/manufacturer"; XPath xPath = XPathFactory.newInstance().newXPath(); assertEquals("toyota", xPath.evaluate(xpath, new InputSource(new StringReader(xml)))); }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); System.out.println(guestName); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date")); }//from w w w . jav a 2 s. c om }
From source file:GuestList.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); String guestCredit = xPath.evaluate("guest/credit", show); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName + " (" + guestCredit + ")"); }//from www .j a va 2 s .c o m }
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()); }/*from w ww.j a v a 2 s . c o m*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory xpf = XPathFactory.newInstance(); XPath xPath = xpf.newXPath(); InputSource inputSource = new InputSource(new StringReader(XML)); String result = (String) xPath.evaluate("//gender", inputSource, XPathConstants.STRING); System.out.println(result);//from www .j a va 2s .c om }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml")))); String expression = "//Home/data"; XPathExpression xPathExpression = xPath.compile(expression); NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET); xPathExpression = xPath.compile("@type"); for (int i = 0; i < elem1List.getLength(); i++) { System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING)); }/*w w w . ja v a 2 s .c o m*/ }