码迷,mamicode.com
首页 > 其他好文 > 详细

xml的信息封装到对象

时间:2016-11-26 17:44:49      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:out   element   demo   throw   attribute   sax   ast   dem   list   

1、xml文件

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="03.contact.css"?>
<contactList>
    <contact id="001">
        <name>张三</name>
        <age>20</age>
        <phone>134222223333</phone>
        <email>zhangsan@qq.com</email>
        <qq>432221111</qq>
    </contact>
    <contact id="003">
        <name>lisi</name>
        <age>20</age>
        <phone>134222225555</phone>
        <email>lisi@qq.com</email>
        <qq>432222222</qq>
    </contact>
</contactList>

2、编写contact类

package cd.itcast.xml;

public class Contact {
    private String id;
    private String name;
    private String age;
    private String phone;
    private String email;
    private String qq;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    @Override
    public String toString() {
        return "Contact [id=" + id + ", name=" + name + ", age=" + age
                + ", phone=" + phone + ", email=" + email + ", qq=" + qq + "]";
    }
}

3、读取xml文件信息,写入对象中

package cd.itcast.xml;

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;
/*
 * 把xml文件信息封装到对象中
 * 2016/11/26
 */
public class Demo4 {
    public static void main(String[] args) throws Exception {
        //有多个对象,所以先放入list
        List<Contact> list = new ArrayList<Contact>();
        //读取xml文件
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File("./src/contact.xml"));
        //读取contact
        Iterator<Element> it= doc.getRootElement().elementIterator("contact");
        while (it.hasNext()) {
            Element element= it.next();
            Contact contact = new Contact();
            contact.setId(element.attributeValue("id"));
            contact.setName(element.elementText("name"));
            contact.setAge(element.elementText("age"));
            contact.setPhone(element.elementText("phone"));
            contact.setEmail(element.elementText("email"));
            contact.setQq(element.elementText("qq"));
            //加入集合中
            list.add(contact);
        }
        //遍历出对象
        for(Contact contact : list){
            System.out.println(contact);
        }
    }
}

 

xml的信息封装到对象

标签:out   element   demo   throw   attribute   sax   ast   dem   list   

原文地址:http://www.cnblogs.com/h-g-f-s123/p/6104590.html

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