当前位置:首页 / 网页制作 / XML和XSLT教程 / java通过XPath解析xml节点的代码介绍
java通过XPath解析xml节点的代码介绍
芯晴素材特效 分类:XML和XSLT教程 发布日期:2018-04-16
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:

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

输出:

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

版权信息:本站所有资源仅供学习与参考,请勿用于商业用途,如有侵犯您的版权,请及时联系821794221#qq.com(#换@),我们将尽快处理。

您可能在找这些

  • 内容标签:

热门素材

跟随鼠标的多彩幻影方格

2013-01-08   浏览:8581

jQuery左右分页滑动

2017-03-19   浏览:197

JS封装函数打造竖向滑动的图片切换效

2013-01-06   浏览:7971

设计漂亮的竖向折叠菜单,浏览文件夹

2013-01-13   浏览:9711

jQuery+CSS3小鸟飞翔动画特效

2017-03-14   浏览:291

点击按钮弹出背景变暗的可拖动的DI

2013-01-06   浏览:8849

jQuery鼠标移动发出气泡动画

2017-03-14   浏览:278

jQuery表单输入城市查询天气预报网页下

2017-03-13   浏览:306

一款可控制切换的图片幻灯切换效果代

2013-01-12   浏览:9458

CSS3动画过渡全屏导航特效

2017-03-14   浏览:259

jQuery响应式回款计划日历表网页下载

2017-03-13   浏览:181

jQuery炫酷鼠标滑过图片显示遮罩层特效

2017-03-14   浏览:386