zoukankan      html  css  js  c++  java
  • [java开发篇][dom4j模块] 遍历xml文件

    http://blog.csdn.net/chenleixing/article/details/44353491

    在android studio 导入dom4j库(build-gradle(Moudle:***)

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support:support-annotations:24.0.0'
        androidTestCompile 'com.android.support.test:runner:0.4.1'
        androidTestCompile 'com.android.support.test:rules:0.4.1'
        compile 'com.android.support:appcompat-v7:24.0.0'
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
        compile 'dom4j:dom4j:20040902.021138'
    }

    被测试的xml文件test.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <root>  
        <user editor="chenleixing" date="2015-02-15">  
            <name>张三</name>  
            <year>24</year>  
            <sex></sex>  
        </user>  
        <user editor="zhangxiaochao" date="2015-02-15">  
            <name>李四</name>  
            <year>24</year>  
            <sex></sex>  
        </user>  
    </root>
     /** 
     * 获取文件的document对象,然后获取对应的根节点 
     * @author  
     */  
    @Test  
    public void testGetRoot() throws Exception{  
        SAXReader sax=new SAXReader();//创建一个SAXReader对象  
        File xmlFile=new File("d:\test.xml");//根据指定的路径创建file对象  
        Document document=sax.read(xmlFile);//获取document对象,如果文档无节点,则会抛出Exception提前结束  
        Element root=document.getRootElement();//获取根节点  
        this.getNodes(root);//从根节点开始遍历所有节点  
      
    }
     /** 
     * 从指定节点开始,递归遍历所有子节点 
     * @author*/  
    public void getNodes(Element node){  
        System.out.println("--------------------");  
          
        //当前节点的名称、文本内容和属性  
        System.out.println("当前节点名称:"+node.getName());//当前节点名称  
        System.out.println("当前节点的内容:"+node.getTextTrim());//当前节点名称  
        List<Attribute> listAttr=node.attributes();//当前节点的所有属性的list  
        for(Attribute attr:listAttr){//遍历当前节点的所有属性  
            String name=attr.getName();//属性名称  
            String value=attr.getValue();//属性的值  
            System.out.println("属性名称:"+name+"属性值:"+value);  
        }  
          
        //递归遍历当前节点所有的子节点  
        List<Element> listElement=node.elements();//所有一级子节点的list  
        for(Element e:listElement){//遍历所有一级子节点  
            this.getNodes(e);//递归  
        }  
    }
    --------------------  
    当前节点名称:root  
    当前节点的内容:  
    --------------------  
    当前节点名称:user  
    当前节点的内容:  
    属性名称:editor属性值:chenleixing  
    属性名称:date属性值:2015-02-15  
    --------------------  
    当前节点名称:name  
    当前节点的内容:张三  
    --------------------  
    当前节点名称:year  
    当前节点的内容:24  
    --------------------  
    当前节点名称:sex  
    当前节点的内容:男  
    --------------------  
    当前节点名称:user  
    当前节点的内容:  
    属性名称:editor属性值:zhangxiaochao  
    属性名称:date属性值:2015-02-15  
    --------------------  
    当前节点名称:name  
    当前节点的内容:李四  
    --------------------  
    当前节点名称:year  
    当前节点的内容:24  
    --------------------  
    当前节点名称:sex  
    当前节点的内容:女
  • 相关阅读:
    LeetCode OJ--Best Time to Buy and Sell Stock II
    LeetCode OJ--Best Time to Buy and Sell Stock
    路飞学城Python-Day37(practise)
    路飞学城Python-Day37
    路飞学城Python-Day36
    路飞学城Python-Day35
    路飞学城Python-Day35
    “肥宅快乐数”-python暴力版
    路飞学城Python-Day34
    路飞学城Python-Day33
  • 原文地址:https://www.cnblogs.com/liuzhipenglove/p/7232397.html
Copyright © 2011-2022 走看看