java dom4j解析xml实例(3)

代码运行前需要先导入dom4j架包。

需要解析的XML文件test.xml如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<students> 
    <student age="25"><!--如果没有age属性,默认的为20--> 
        <name>崔卫兵</name> 
        <college>PC学院</college> 
        <telephone>62354666</telephone> 
        <notes>男,1982年生,硕士,现就读于北京邮电大学</notes>
    </student> 
    <student age="26"
        <name>cwb</name> 
        <college leader="学院领导">PC学院</college><!--如果没有leader属性,默认的为leader-->
        <telephone>62358888</telephone> 
        <notes>男,1987年生,硕士,现就读于中国农业大学</notes> 
    </student>
</students>

 

<1>、当测试文件test.xml在D盘时,Java程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Test01;
 
import java.io.FileNotFoundException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class test01 {
 
 
    public void testRead() throws DocumentException, FileNotFoundException{
        SAXReader reader=new SAXReader();
 
        Document doc=reader.read("D:/test.xml");
        Element root =doc.getRootElement();
        for(@SuppressWarnings("rawtypes")
        Iterator it=root.elementIterator();it.hasNext();){
            Element element=(Element)it.next();
 
            System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
            System.out.println(element.attributeValue("age"));
            System.out.println(element.getName());
            for(@SuppressWarnings("rawtypes")
            Iterator itt=element.elementIterator();itt.hasNext();){
                //System.out.println(element.attributeValue("age"));
                Element el=(Element)itt.next();
                System.out.println(el.getName()+"=="+el.getText()); 
                //getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
                //getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
                //attributeValue("age")可获取age的值即25
            }
            System.out.println("==================");          
        }
    }
     
    public static void main(String[] args){
        try {
            new test01().testRead();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

代码运行后控制台显示结果如下:

 

<2>、当测试文件test.xml和项目在一起时,Java程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package Test01;
 
import java.io.FileNotFoundException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class test01 {
 
 
    public static void testRead(String path) throws DocumentException, FileNotFoundException{
        SAXReader reader=new SAXReader();
 
        //Document doc=reader.read("D:/test.xml");
        Document doc = reader.read(path);
        Element root =doc.getRootElement();
        for(@SuppressWarnings("rawtypes")
        Iterator it=root.elementIterator();it.hasNext();){
            Element element=(Element)it.next();
 
            System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
            System.out.println(element.attributeValue("age"));
            System.out.println(element.getName());
            for(@SuppressWarnings("rawtypes")
            Iterator itt=element.elementIterator();itt.hasNext();){
                //System.out.println(element.attributeValue("age"));
                Element el=(Element)itt.next();
                System.out.println(el.getName()+"=="+el.getText()); 
                //getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
                //getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
                //attributeValue("age")可获取age的值即25
            }
            System.out.println("==================");          
        }
    }
     
    public static void main(String[] args){
//      try {
//          new test01().testRead();
//      } catch (Exception e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
        try {
            testRead("test.xml");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

代码运行后控制台显示结果如下:

posted @   岁月淡忘了谁  阅读(190)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示