Chinaunix首页 | 论坛 | 博客
  • 博客访问: 488307
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: Java

2014-08-22 16:39:57

java使用XPath查找xml节点

点击(此处)折叠或打开

  1. package cn.outofmemory.snippets.core;

  2. import java.io.File;
  3. import java.io.FileInputStream;

  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import javax.xml.xpath.XPath;
  7. import javax.xml.xpath.XPathConstants;
  8. import javax.xml.xpath.XPathFactory;

  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Node;
  11. import org.w3c.dom.NodeList;

  12. public class FindElementsByAbsoluteLocationWithXPath {

  13.     public static void main(String[] args) throws Exception {

  14.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  15.         dbf.setValidating(false);
  16.         DocumentBuilder db = dbf.newDocumentBuilder();

  17.         Document doc = db.parse(new FileInputStream(new File("in.xml")));

  18.         XPathFactory factory = XPathFactory.newInstance();

  19.         XPath xpath = factory.newXPath();

  20.         String expression;
  21.         Node node;
  22.         NodeList nodeList;

  23.         // 1. root element
  24.         expression = "/*";
  25.         node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
  26.         System.out.println("1. " + node.getNodeName());

  27.         // 2. root element (by name)
  28.         expression = "/rss";
  29.         node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
  30.         System.out.println("2. " + node.getNodeName());

  31.         // 3. element under rss
  32.         expression = "/rss/channel";
  33.         node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
  34.         System.out.println("3. " + node.getNodeName());

  35.         // 4. all elements under rss/channel
  36.         expression = "/rss/channel/*";
  37.         nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
  38.         System.out.print("4. ");
  39.         for (int i = 0; i < nodeList.getLength(); i++) {
  40.             System.out.print(nodeList.item(i).getNodeName() + " ");
  41.         }
  42.         System.out.println();

  43.         // 5. all title elements in the document
  44.         expression = "//title";
  45.         nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
  46.         System.out.print("5. ");
  47.         for (int i = 0; i < nodeList.getLength(); i++) {
  48.             System.out.print(nodeList.item(i).getNodeName() + " ");
  49.         }
  50.         System.out.println();

  51.         // 6. all elements in the document except title
  52.         expression = "//*[name() != 'title']";
  53.         nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
  54.         System.out.print("6. ");
  55.         for (int i = 0; i < nodeList.getLength(); i++) {
  56.             System.out.print(nodeList.item(i).getNodeName() + " ");
  57.         }
  58.         System.out.println();

  59.         // 7. all elements with at least one child element
  60.         expression = "//*[*]";
  61.         nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
  62.         System.out.print("7. ");
  63.         for (int i = 0; i < nodeList.getLength(); i++) {
  64.             System.out.print(nodeList.item(i).getNodeName() + " ");
  65.         }
  66.         System.out.println();

  67.         // 8. all level-5 elements (the root being at level 1)
  68.         expression = "/*/*/*/*";
  69.         nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
  70.         System.out.print("8. ");
  71.         for (int i = 0; i < nodeList.getLength(); i++) {
  72.             System.out.print(nodeList.item(i).getNodeName() + " ");
  73.         }
  74.         System.out.println();

  75.     }

  76. }
Input:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <rss version="2.0">
  3.     <channel>
  4.         <title>Java Tutorials and Examples 2</title>
  5.         <language>en-us</language>
  6.         <item>
  7.             <title><![CDATA[Java Tutorials 2]]></title>
  8.             <link>http://www.javacodegeeks.com/</link>
  9.         </item>
  10.         <item>
  11.             <title><![CDATA[Java Examples 2]]></title>
  12.             <link>http://examples.javacodegeeks.com/</link>
  13.         </item>
  14.     </channel>
  15. </rss>
输出:

点击(此处)折叠或打开

  1. 1. rss
  2. 2. rss
  3. 3. channel
  4. 4. title language item item
  5. 5. title title title
  6. 6. rss channel language item link item link
  7. 7. rss channel item item
  8. 8. title link title link



阅读(1216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~