java通过dom4j解析xml文件

java解析xml文件有很多种(dom4j、jdom)等等,下面记录下自己通过dom4j解析的xml的方法

1·首先解析的项目目录

2· 使用dom4j解析需要依赖的jar包就一个dom4j.jar包

点击下载

3·解析的代码如下:

package com.util;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSON;
import com.bean.XmlBean;

public class ParseXmlUtil {
	public void parsexml() throws Exception{
		SAXReader reader = new SAXReader();
		/*读取resource.xml文件*/
		Document document = reader.read("src/resource.xml");
		/*获取xml文件的根节点*/
		Element rootElement = document.getRootElement();
		/*将获取到的根节点下的resourceitem节点放在list中*/
		List items = rootElement.elements("resourceitem");
		 /*创建source存放每一个resourceitem中资源*/  
		List<XmlBean> beans = new ArrayList<XmlBean>();
		for(int i = 0 ; i < items.size() ; i++){
			XmlBean bean = new XmlBean();
			Element resourceitem = (Element)items.get(i);
			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();
			bean.setAuthor(author);
			bean.setDate(date);
			bean.setDescribe(describe);
			bean.setId(id);
			bean.setKeywords(keywords);
			bean.setKind(kind);
			bean.setTitle(title);
			bean.setUrl(url);
			beans.add(bean);
		}
		System.out.println(JSON.toJSONString(beans));
		
	}
}

4·我的资源文件的resource.xml文件

<?xml version="1.0" encoding="UTF-8"?>  
  
<allresource host="192.168.0.81" remote="192.168.0.82">   
  <resourceitem>   
    <id>1</id>    
    <title>锋利的jquery</title>    
    <keywords>jquery</keywords>    
    <kind>chm</kind>    
    <describe>详细描写jquery的书</describe>    
    <date>2015-03-25 00:17:01</date>    
    <url>http://192.168.0.81/book</url>    
    <author>释然</author>    
    <publisher>人民出版社</publisher>   
  </resourceitem>    
  <resourceitem>   
    <id>2</id>    
    <title>疯狂java基础讲义</title>    
    <keywords>java</keywords>    
    <kind>chm</kind>    
    <describe>讲解java的入门书籍</describe>    
    <date>2015-03-25 00:19:10</date>    
    <url>http://192.168.0.81:8080/</url>    
    <author>shiran</author>    
    <publisher>人民出版社</publisher>   
  </resourceitem>
</allresource>


5·需要创建的bean类

package com.bean;

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 getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	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;
	}
    
}

6·进行测试:
package com.test;

import com.util.ParseXmlUtil;

public class TestParseXml {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ParseXmlUtil xmlUtil = new ParseXmlUtil();
		try {
			xmlUtil.parsexml();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

7·结果 :
[{"author":"释然","date":"2015-03-25 00:17:01","describe":"详细描写jquery的书","id":"1","keywords":"jquery","kind":"chm","title":"锋利的jquery","url":"http://192.168.0.81/book"},
{"author":"shiran","date":"2015-03-25 00:19:10","describe":"讲解java的入门书籍","id":"2","keywords":"java","kind":"chm","title":"疯狂java基础讲义","url":"http://192.168.0.81:8080/"}]

8·完成


demo下载

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值