码迷,mamicode.com
首页 > 编程语言 > 详细

java解析xml文件

时间:2014-07-16 20:01:58      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   使用   

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

ParseXml类如下:

 

package com.java.team;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
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中
        List list  = root.elements("resourceitem");
        //创建source存放每一个resourceitem中资源
        List<XmlBean> source = new ArrayList<XmlBean>();
        //将resourceitem中的各项解析出来,通过XmlBean存放到source中
        for(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);
        }
   
	}

}

 

 

XmlBean类如下:

 

package com.java.team;

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;
	}

}

 

 

xml文件如下:

 

<?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>

 

 

 

 

 

 

java解析xml文件,布布扣,bubuko.com

java解析xml文件

标签:des   style   blog   http   java   使用   

原文地址:http://www.cnblogs.com/henuyuxiang/p/3837214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!