当前位置: 移动技术网 > IT编程>开发语言>C/C++ > XPath读取xml文件

XPath读取xml文件

2019年06月15日  | 移动技术网IT编程  | 我要评论

家有爹娘第一部,时空穿梭大军阀,奥姆真理教

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网