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());

猜你喜欢

转载自www.cnblogs.com/fmain/p/11024064.html