dom解析xml,会一次性读入xml文件,会把文件中的所有元素,解析成一个个Node对象节点。dom解析是基于树和节点的文档,通过操作节点的相关方法,可以获取节点的属性,节点的内容,也可以获节点的子节点,如此便可以对xml文件进行解析。
所以说dom解析便于操作,可以轻松完成增删改查,
不过dom解析的文档不适合过大,由于它是将文档读入内存之中,
过大的文件影响了机器性能,甚至容易导致内存溢出。
Node对象提供了很多常量,表示当前结点的类型,基于Node对象的操作会相对比较复杂,所以我们可以把Node转成其对应子类,子类里提供了很多便捷的方法去获取数据。如把Node类型的对象转成Element对象。
以下是一个person.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="328">
<name>zhangsan</name>
<age>23</age>
</person>
<person id="303">
<name>lisi</name>
<age>22</age>
</person>
</persons>
对应的Person封装类,将读取的数据封装成Person类:
public class Person {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Person() {
super();
}
}
在使用java api对xml文件进么解析的时候,由于解析xml文件的方式相对较多,类名相同的相关类也比较多,所以导包的时候要多注意,以免造成不必要的错误。
import java.io.IOException;
import java.io.InputStream;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomTest {
public static void main(String[] args) {
List<Person> list = new DomTest().parsePersons();
//打印读取的结果。
for (Person p : list) {
System.out.println(p);
}
}
//解析xml文件,并把结果封装至Person的集合中返回
private List<Person> parsePersons() {
List<Person> persons = null;
try {
//获得dom制造工厂
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
//获得dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//获得xml文件的输入流
InputStream inStream = DomTest.class.getClassLoader()
.getResourceAsStream("person.xml");
//通过解析器得到Document对象
Document document = builder.parse(inStream);
//得到要获取的所有person结点,通过每个结点,获取每个person的内容数据。
NodeList list = document.getElementsByTagName("person");
persons = new ArrayList<Person>();
Person person = null;
//遍历person集合,将数据封装于person对象中
for (int i = 0, size = list.getLength(); i < size; i++) {
person = new Person();
//得到<person>结点
Element e = (Element) list.item(i);
//获取其id属性
String id = e.getAttribute("id");
person.setId(Integer.valueOf(id));
//得到当前<person>结点的所有孩子结点。
NodeList chileList = e.getChildNodes();
//遍历所有孩子结点,以便获得其余数据。
for (int j = 0, csize = chileList.getLength(); j < csize; j++) {
Node node = chileList.item(j);
switch (node.getNodeName()) {
case "name":
//当结点为<name>时,获取内容,并给person赋值
String name = node.getTextContent();
person.setName(name);
break;
case "age":
//当结点为<age>时,获取内容,并给person赋值
String age = node.getTextContent();
person.setAge(Integer.valueOf(age));
break;
}
}
//完成一个<person>结点的遍历,将person对象加入集合中。
persons.add(person);
//清空person对象的数据。
person = null;
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//返回最终得到的数据。
return persons;
}
}