XML之DOM解析

import java.io.IOException;
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 + "]";
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值