标签:des style blog http java 使用
使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包。
ParseXml类代码如下:
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类代码如下:
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文件内容如下:
<?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>
运行结果(我只让他输出了部分值)如下图所示:
标签:des style blog http java 使用
原文地址:http://www.cnblogs.com/henuyuxiang/p/3837234.html