java解析xml文件并输出

使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包。

ParseXml类代码如下:

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
57
58
59
60
61
62
63
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class ParseXml {
    public void read() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/test.xml"));
        Element root = document.getRootElement();
        // 将解析出来的allresource下的resourceitem放在list中
        @SuppressWarnings("rawtypes")
        List list = root.elements("resourceitem");
        // 创建source存放每一个resourceitem中资源
        List<XmlBean> source = new ArrayList<XmlBean>();
        // 将resourceitem中的各项解析出来,通过XmlBean存放到source中
        for (@SuppressWarnings("rawtypes")
        Iterator i = list.iterator(); i.hasNext();) {
            Element resourceitem = (Element) i.next();
            String id = resourceitem.element("id").getText();
            String title = resourceitem.element("title").getText();
            String keywords = resourceitem.element("keywords").getText();
            String kind = resourceitem.element("kind").getText();
            String describe = resourceitem.element("describe").getText();
            String date = resourceitem.element("date").getText();
            String url = resourceitem.element("url").getText();
            String author = resourceitem.element("author").getText();
            String publisher = resourceitem.element("publisher").getText();
            XmlBean bean = new XmlBean();
            bean.setId(id);
            bean.setTitle(title);
            bean.setKeywords(keywords);
            bean.setKind(kind);
            bean.setDescribe(describe);
            bean.setDate(date);
            bean.setUrl(url);
            bean.setAuthor(author);
            bean.setPublisher(publisher);
            source.add(bean);
        }
        //这里只选两个解析出的结果进行输出
        for (int i = 0; i < source.size(); i++) {
            System.out.println("date数据为:"+source.get(i).getDate());
            //System.out.println("url数据为:"+source.get(i).getUrl());
        }  
        for (int i = 0; i < source.size(); i++) {
            //System.out.println("date数据为:"+source.get(i).getDate());
            System.out.println("url数据为:"+source.get(i).getUrl());
        }  
    }
     
    //主函数调用上面read()方法
    public static void main(String[] args){
        try {
        new ParseXml().read();
        } catch (Exception e){
            e.printStackTrace();
        }
        }
}

 

XmlBean类代码如下:

 

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class XmlBean { 
    private String id; 
    private String title; 
    private String keywords; 
    private String kind; 
    private String describe; 
    private String date; 
    private String url; 
    private String author; 
    public String getKeywords() { 
        return keywords; 
    
   
    public void setKeywords(String keywords) { 
        this.keywords = keywords; 
    
   
    public String getKind() { 
        return kind; 
    
   
    public void setKind(String kind) { 
        this.kind = kind; 
    
   
    public String getDescribe() { 
        return describe; 
    
   
    public void setDescribe(String describe) { 
        this.describe = describe; 
    
   
    public String getDate() { 
        return date; 
    
   
    public void setDate(String date) { 
        this.date = date; 
    
   
    public String getUrl() { 
        return url; 
    
   
    public void setUrl(String url) { 
        this.url = url; 
    
   
    public String getAuthor() { 
        return author; 
    
   
    public void setAuthor(String author) { 
        this.author = author; 
    
   
    public String getPublisher() { 
        return publisher; 
    
   
    public void setPublisher(String publisher) { 
        this.publisher = publisher; 
    
   
    private String publisher; 
   
    public String getId() { 
        return id; 
    
   
    public void setId(String id) { 
        this.id = id; 
    
   
    public String getTitle() { 
        return title; 
    
   
    public void setTitle(String title) { 
        this.title = title; 
    

 

 

test.xml文件内容如下:

 

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
<?xml version="1.0" encoding="UTF-8"?> 
   
<allresource host="192.168.16.111" remote="192.168.16.111">  
  <resourcenum>499</resourcenum>   
  <resourceitem>  
    <id>2</id>   
    <title>《实数》复习d.doc</title>   
    <keywords>实数复习,期中复习</keywords>   
    <kind>doc</kind>   
    <describe>期中复习训练题</describe>   
    <date>2008-6-18 20:50:01</date>   
    <url>http://192.168.16.111:8080/resources/《实数》复习d.doc</url>   
    <author>胡克林</author>   
    <publisher>历城二中</publisher>  
  </resourceitem>   
  <resourceitem>  
    <id>3</id>   
    <title>《轴对称图形》教学设计</title>   
    <keywords>轴对称图形,平面几何图形</keywords>   
    <kind>doc</kind>   
    <describe>认识轴对称图形,知道轴对称图形的含义,能够找出轴对称图形的对称轴</describe>   
    <date>2008-6-18 20:55:10</date>   
    <url>http://192.168.16.111:8080/resources/《轴对称图形》教学设计.doc</url>   
    <author>胡克林</author>   
    <publisher>历城二中</publisher>  
  </resourceitem>   
  </allresource>

 

 

运行结果(我只让他输出了部分值)如下图所示:

posted @   岁月淡忘了谁  阅读(1541)  评论(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
点击右上角即可分享
微信分享提示