XML解析之JDOM

前端之家收集整理的这篇文章主要介绍了XML解析之JDOM前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


/**
*
* JDOM解析XML
*
*/
public class JDomParserDemo {

public static void main(String[] args) throws JDOMException,IOException {

//1.获取JDOM解析器对象
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("person.xml"));
Element root = document.getRootElement();
List rootchilds = root.getChildren();
Person p;
for (int i = 0; i < rootchilds.size(); i++) {
Element person = (Element) rootchilds.get(i);
p = new Person();
p.setId(person.getAttributeValue("personid"));
p.setName(person.getChildText("name"));
p.setAddress(person.getChildText("address"));
p.setTel(person.getChildText("tel"));
p.setFax(person.getChildText("fax"));
p.setEmail(person.getChildText("email"));
System.out.println(p);
}
}

}

************************************************************************

Person类

public class Person { private String id; private String name; private String address; private String tel; private String fax; private String email; public Person() { super(); // TODO Auto-generated constructor stub } public Person(String id,String name,String address,String tel,String fax,String email) { super(); this.id = id; this.name = name; this.address = address; this.tel = tel; this.fax = fax; this.email = email; } public String getId() { return id; } public String getName() { return name; } public String getAddress() { return address; } public String getTel() { return tel; } public String getFax() { return fax; } public String getEmail() { return email; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } public void setTel(String tel) { this.tel = tel; } public void setFax(String fax) { this.fax = fax; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Person [id=" + id + ",name=" + name + ",address=" + address + ",tel=" + tel + ",fax=" + fax + ",email=" + email + "]"; } } 原文链接:https://www.f2er.com/xml/294845.html

猜你在找的XML相关文章