栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > 其他 > xml语言

java通过XPath解析xml节点的代码详解

xml语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java通过XPath解析xml节点的代码详解

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 = "*/*/*";        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/                            <![CDATA[Java Examples 2]]>            http://examples.javacodegeeks.com/            

输出:

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

以上就是java通过XPath解析xml节点的代码详解的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/794342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号