How To Role Xpath To Read Nodes From Xml File

XPath is a coffee library that permit us read a complicated xml document amongst ease.

In our elementary representative below nosotros bring an xml that contains computers amongst closed to random tags similar os.

<computers>  <windows>   <lenovo model="g50">    <year>2015</year>   </lenovo>   <lenovo model="g5040">    <year>2014</year>   </lenovo>   <asus>    <year>2015</year>   </asus>   <dell>    <year>2014</year>   </dell>  </windows>  <osx>   <macbookpro>    <year>2014</year>   </macbookpro>  </osx> </computers> 

For representative if nosotros desire to become the lenovo node amongst yr 2014, nosotros as well as thence require to purpose the xpath characteristic amongst a parameter=/computers/windows/lenovo[year/text()='2014'].

package com.broodcamp.xstream_demo;  import java.io.File; import java.io.IOException; import java.io.StringWriter;  import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory;  import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException;  /**  * @author Edward P. Legaspi  **/ populace degree XPathDemo {   populace static void main(String[] args) {   endeavour {    novel XPathDemo();   } grab (XPathExpressionException | ParserConfigurationException | SAXException | IOException     | TransformerException e) {    // TODO Auto-generated grab block    e.printStackTrace();   }  }   populace XPathDemo() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException,    TransformerException {   ClassLoader classLoader = getClass().getClassLoader();   File fXmlFile = novel File(classLoader.getResource("xpath_demo.xml").getFile());    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();   Document doctor = dBuilder.parse(fXmlFile);    Transformer trans = TransformerFactory.newInstance().newTransformer();   StringWriter author = novel StringWriter();   trans.transform(new DOMSource(doc), novel StreamResult(writer));   System.out.println(writer.getBuffer().toString().replaceAll("\n|\r", ""));    XPath xPath = XPathFactory.newInstance().newXPath();   XPathExpression expr = xPath.compile("/computers/windows/lenovo[year/text()='2014']");   Object outcome = expr.evaluate(doc, XPathConstants.NODE);    NodeList nodes = (NodeList) result;    for (int i = 0; i < nodes.getLength(); i++) {    System.out.println("nodes: " + nodes.item(i).getTextContent());   }  }  } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.