Detailed explanation of java code for parsing xml nodes through XPath

Source: Internet
Author: User
Tags cdata
Code snippets, code sharing, PHP code sharing, Java code sharing, Ruby code sharing, Python code sharing, HTML code sharing, CSS code sharing, SQL code sharing, and JavaScript code sharing
import java.io.File;import java.io.FileInputStream; import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList; public class FindElementsByAbsoluteLocationWithXPath {     public static void main(String[] args) throws Exception {         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        dbf.setValidating(false);        DocumentBuilder db = dbf.newDocumentBuilder();         Document doc = db.parse(new FileInputStream(new File("in.xml")));         XPathFactory factory = XPathFactory.newInstance();         XPath xpath = factory.newXPath();         String expression;        Node node;        NodeList nodeList;         // 1. root element        expression = "/*";        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);        System.out.println("1. " + node.getNodeName());         // 2. root element (by name)        expression = "/rss";        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);        System.out.println("2. " + node.getNodeName());         // 3. element under rss        expression = "/rss/channel";        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);        System.out.println("3. " + node.getNodeName());         // 4. all elements under rss/channel        expression = "/rss/channel/*";        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);        System.out.print("4. ");        for (int i = 0; i < nodeList.getLength(); i++) {            System.out.print(nodeList.item(i).getNodeName() + " ");        }        System.out.println();         // 5. all title elements in the document        expression = "//title";        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);        System.out.print("5. ");        for (int i = 0; i < nodeList.getLength(); i++) {            System.out.print(nodeList.item(i).getNodeName() + " ");        }        System.out.println();         // 6. all elements in the document except title        expression = "//*[name() != 'title']";        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);        System.out.print("6. ");        for (int i = 0; i < nodeList.getLength(); i++) {            System.out.print(nodeList.item(i).getNodeName() + " ");        }        System.out.println();         // 7. all elements with at least one child element        expression = "//*[*]";        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);        System.out.print("7. ");        for (int i = 0; i < nodeList.getLength(); i++) {            System.out.print(nodeList.item(i).getNodeName() + " ");        }        System.out.println();         // 8. all level-5 elements (the root being at level 1)        expression = "/*/*/*/*";        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);        System.out.print("8. ");        for (int i = 0; i < nodeList.getLength(); i++) {            System.out.print(nodeList.item(i).getNodeName() + " ");        }        System.out.println();     } }

Input:

 
     
          
   Java Tutorials and Examples 2        
   
    en-us
           
               
    <![CDATA[Java Tutorials 2]]>            
    http://www.javacodegeeks.com/</link>        
           
               
    <![CDATA[Java Examples 2]]>            
    http://examples.javacodegeeks.com/</link>        
       
  
 

Output:

1. rss2. rss3. channel4. title language item item5. title title title6. rss channel language item link item link7. rss channel item item8. title link title link

The above is the detailed explanation of java code for parsing xml nodes through XPath. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.