优点:使用DOM解析XML的代码看起来比较直观,并且在某些方面比基于SAX的实现更加简单。
缺点:DOM需要将XML文件的所有内容以文档树的方式存放在内存中,所以内存的消耗比较大。
使用场合:如果XML文件的内容比较小,采用DOM也是可行的。
DOM解析XML文件的基本思路如下:
利用DocumentBuilderFactory创建一个DocumentBuilderFactory实例。
利用DocumentBuilderFactory创建DocumentBuilder.
加载XML文档(Document)
获取文档的根节点(Element)
获取根节点中所有子节点的列表(NodeList)
获取子节点列表中的需要读取的节点
Node接口的主要方法:
方法 | 释义 |
---|---|
Short getNodeType() | 该方法返回一个节点类型的常量,如对于Element节点,getNodeType()方法返回的值为:Node.ELEMENT_NODE |
NodeList getChildNodes() |
返回一个由当前节点的所有子节点组成的NodeList对象 |
Node getFirstChild() | 返回当前节点的第一个子节点 |
Node getLastChild() | 返回当前的最后一个子节点 |
NodeList getTextContent() | 返回当前节点及所有子孙节点中的文本内容 |
具体代码如下:
domain:
package com.wangjialin.domain; public class Person { private Integer id; private String name; private Short age; public Person() { } public Person(Integer id, String name, Short age) { this.id = id; this.name = name; this.age = age; } public Person(String name, Short age) { this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } @Override public String toString() { return "id=" + id + ",name=" + name + ",age=" + age; } }
service:
package com.wangjialin.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.wangjialin.domain.Person; public class DOMPersonService { public static List<Person> getPersons(InputStream inStream) throws Exception { List<Person> persons = new ArrayList<Person>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inStream); Element root = document.getDocumentElement(); NodeList personNodes = root.getElementsByTagName("person"); for (int i = 0; i < personNodes.getLength(); i++) { Element personElement = (Element) personNodes.item(i); int id = new Integer(personElement.getAttribute("id")); Person person = new Person(); person.setId(id); NodeList childNodes = personElement.getChildNodes(); for (int y = 0; y < childNodes.getLength(); y++) { if (childNodes.item(y).getNodeType() == Node.ELEMENT_NODE) { if ("name".equals(childNodes.item(y).getNodeName())) { String name = childNodes.item(y).getFirstChild() .getNodeValue(); person.setName(name); } else if ("age".equals(childNodes.item(y).getNodeName())) { String age = childNodes.item(y).getFirstChild() .getNodeValue(); person.setAge(new Short(age)); } } } persons.add(person); } inStream.close(); return persons; } }
Activity或Test类:
package com.wangjialin.xml; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.test.AndroidTestCase; import android.util.Log; import com.wangjialin.domain.Person; import com.wangjialin.service.DOMPersonService; public class PersonServiceTest extends AndroidTestCase { private static final String TAG = "PersonServiceTest"; public void testDOMgetPersons() throws Throwable { InputStream inStream = this.getClass().getClassLoader() .getResourceAsStream("wangjialin.xml"); List<Person> persons = DOMPersonService.getPersons(inStream); for (Person person : persons) { Log.i(TAG, person.toString()); } } }
xml:
wangjialin.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id = "23"> <name>NEwii</name> <age>21</age> </person> <person id = "20"> <name>WiiWill</name> <age>25</age> </person> </persons>