设为首页 加入收藏

TOP

XPath读取xml文件
2019-06-14 18:08:24 】 浏览:59
Tags:XPath 读取 xml 文件

1.创建解析工厂

2.创建解析器

3.读xml文件,生成w3c.docment对象树

4.创建XPath对象

5.通过路径查找对象

例子:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

public class MyXPathTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        try {
            //创建解析工厂
            DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
            //创建解析器
            DocumentBuilder builder=documentBuilderFactory.newDocumentBuilder();
            //通过解析器读取文件,生成w3c.dom.Document象树
            Document document=builder.parse("conf/55.xml");
            //创建XPath对象
            XPath xPath=XPathFactory.newInstance().newXPath();
//            <Model id="057ea377-e531-422d-a181-3371b42e5bd0">
//                <Ref>
//                    <node></node>
//                    <node></node>
//                </Ref>
//                <Ref>
//                    <node></node>
//                    <node></node>
//                </Ref>
//            </Model>
            //读取Model中属性id的值
            String idPath="/Model/@id";
            String id=(String) xPath.eva luate(idPath, document, XPathConstants.STRING);
            System.out.println("id="+id);
            
//            <Model>
//            <Ref>
//                <node></node>
//                <node></node>
//            </Ref>
//            <Ref>
//                <node id="057ea377-e531-422d-a181-3371b42e5bd0" nodetype="DynamicMoleNode"></node>
//                <node></node>
//            </Ref>
//        </Model>
            String idNodePath="/Model/node[@nodetype='DynamicMoleNode']/@id";
            String idNode=(String) xPath.eva luate(idNodePath, document, XPathConstants.STRING);
            System.out.println("idNode="+idNode);
            
//            <Model>
//            <Ref>
//                <node></node>
//                <node></node>
//            </Ref>
//            <Ref>
//                <node id=" nodetype="DynamicMoleNode">
//                    <node rtf="aaaaaaaa" nodetype="Text" />
//                </node>
//                <node></node>
//            </Ref>
//        </Model>
            String rtfPath="/Model/node[@nodetype='DynamicMoleNode']/node[@nodetype='Text']/@rtf";
            String rtf=(String) xPath.eva luate(rtfPath, document, XPathConstants.STRING);
            System.out.println("rtf="+rtf);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

同一xpath路径下有多个Element对象

String path="/XTextDocument/XElements/Element[@type='XTextBody']/XElements/Element";
NodeList nodeList
=(NodeList) xPath.eva luate(path,document, XPathConstants.NODESET); System.out.println("nodeList===="+nodeList.getLength());

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇迭代(二):迭代法求方程的根 下一篇迭代(一):迭代算法的基本思想

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目