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) {
QQ = qQ;
}
@Override
public String toString() {
return "Contac [id=" + id + ", name=" + name + ", age=" + age
+ ", phone=" + phone + ", email=" + email + ", QQ=" + QQ + "]";
}
public static void main(String[] args) throws Exception {
List<Contac> list = new ArrayList<Contac>();
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("./src/oncs.xml"));
//读取contact标签
Iterator<Element> it = doc.getRootElement().elementIterator("contact");
while(it.hasNext()){
Element elem = it.next();
//创建Contact
Contac contact = new Contac();
contact.setId(elem.attributeValue("id"));
contact.setName(elem.elementText("name"));
contact.setAge(elem.elementText("age"));
contact.setPhone(elem.elementText("phone"));
contact.setEmail(elem.elementText("email"));
contact.setQQ(elem.elementText("QQ"));
list.add(contact);
}
for (Contac contact : list) {
System.out.println(contact);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<contactList>
<contact id="001" name="sdsd">
<name>庄三</name>
<age>20</age>
<phone>1534151</phone>
<email>23234sdfsa@sj555</email>
<QQ>646415654</QQ>
</contact>
<contact id="002">
<name>李四</name>
<age>23</age>
<phone>232424</phone>
<email>2324@af.com</email>
<QQ>452156320</QQ>
</contact>
</contactList>
腾不出时间思考的人,迟早会腾出时间来后悔;腾不出时间学习的人,迟早会腾出时间来伤悲。