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.evaluate(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.evaluate(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.evaluate(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.evaluate(path,document, XPathConstants.NODESET); System.out.println("nodeList===="+nodeList.getLength());

 

转载于:https://www.cnblogs.com/fmain/p/11024064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值