XPath read xml file

1. Create a parser factory

2. Create a parser

3. Read xml file, generate w3c.docment object tree

4. Create XPath object

5. Find the object through a path

example:

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) {
        
        the try {
             // create a parser factory 
            the DocumentBuilderFactory DocumentBuilderFactory = DocumentBuilderFactory.newInstance ();
             // create a parser 
            the DocumentBuilder Builder = the DocumentBuilderFactory.newDocumentBuilder ();
             // by reading the file parser generates a tree like w3c.dom.Document 
            Document document = builder.parse ( "the conf / 55.xml" );
             // create XPath object 
            XPath xPath = XPathFactory.newInstance () newXPath ();.
 //             <the 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();
        }
    }

}

 

Element object under a plurality of the same path xpath

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

 

Guess you like

Origin www.cnblogs.com/fmain/p/11024064.html