• java解析xml文件


    使用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>
    
  • 相关阅读:
    软件测试方法
    激情是人生中最可宝贵的
    比较有意思的一篇文章
    基于LNMP的Zabbbix之Zabbix Agent源码详细安装,但不给图
    Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
    502 Bad Gateway(Nginx) 查看nginx日志有如下内容
    jcaptcha进阶
    MFC 程序的运行流程
    Selenium API 介绍
    java.util.ComparableTimSort中的sort()方法简单分析
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3837214.html
Copyright © 2020-2023  润新知