import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* DOM解析:一次性将xml文件以树形结构存放到内容中
*
*
*/
public class DOMParserDemo {
public static void main(String[] args) {
try {
List<Person> ps = domParserMethod();
for (Person person : ps) {
System.out.println(person);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static List<Person> domParserMethod() throws ParserConfigurationException, SAXException, IOException {
//1.获取解析器工厂对象
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//2.获取解析器对象
DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
//3.解析
Document document = db.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("person.xml"));
//准备工作,准备一个集合
List<Person> ps = new ArrayList<Person>();
Person p;
//从document里获取数据
NodeList nodeListPersons = document.getElementsByTagName("person");
System.out.println(nodeListPersons.getLength()+"--->size");
for (int i = 0; i < nodeListPersons.getLength(); i++) {
Node nodePerson = nodeListPersons.item(i);//一个Person
p =new Person();
p.setId(nodePerson.getAttributes().getNamedItem("personid").getNodeValue());//获取person的属性(id)
NodeList chNodeList = nodePerson.getChildNodes();
for (int j = 0; j < chNodeList.getLength();j++) {
Node chNode = chNodeList.item(j);
String name = chNode.getNodeName();
// System.out.println(name);
if(name.equals("name")){
p.setName(chNode.getFirstChild().getNodeValue());
}else if(name.equals("address")){
p.setAddress(chNode.getFirstChild().getNodeValue());
}else if(name.equals("tel")){
p.setTel(chNode.getFirstChild().getNodeValue());
}else if(name.equals("fax")){
p.setFax(chNode.getFirstChild().getNodeValue());
}else if(name.equals("email")){
p.setEmail(chNode.getFirstChild().getNodeValue());
}
}
ps.add(p);
}
return ps;
}
}
********************************************************************
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 + "]";
}
}