DOM解析XML

1、XML文件
<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="23">
		<name>李明</name>
		<age>30</age>
	</person>
	<person id="20">
		<name>李向梅</name>
		<age>25</age>
	</person>
</persons>


2、JAVABean
package com.wynlink.domain;

public class Person {

	private Integer id;
	private String name;
	private Short age;
	
	public Person() {
		super();
	}
	public Person(String name, Short age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person(Integer id, String name, Short age) {
		super();
		this.id = id;
		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;
	}

	//重写toString方法
	@Override
	public String toString() {
		return "id="+id+",name="+name+",age="+age;
	}
	
	
}



3、
package com.wynlink.xml;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import android.test.AndroidTestCase;
import android.util.Log;

import com.wynlink.domain.Person;
import com.wynlink.service.DOMPersonService;
import com.wynlink.service.PullPersonService;
import com.wynlink.service.SAXPersonService;

/**
 * JUnit测试类
 * @author yaodaqing
 *
 */
public class XMLPersonServiceTest extends AndroidTestCase {

	public static final String TAG = "XMLPersonServiceTest";

	//使用DOM方法解析XML内容;适合使用解析比较小的XML内容
	public void testDomReadXML() throws Exception{
		InputStream inStream = XMLPersonServiceTest.class.getClassLoader().getResourceAsStream("wynlink.xml");
		List<Person> persons = DOMPersonService.readxml(inStream);
		for(Person person : persons){
			Log.i(TAG, person.toString());
		}
	}
}


4、
package com.wynlink.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.wynlink.domain.Person;

/**
 * DOM方式解析XML文件
 * 		加载全部的XML进行解析,适用于比较小的XML文件
 * @author yaodaqing
 *
 */
public class DOMPersonService {

	//输入流可以来自于文件系统,也可以来自于网络
	public static List<Person> readxml(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();//根节点persons
		NodeList nodes = root.getElementsByTagName("person");//每一个person节点
		for(int i=0 ; i<nodes.getLength();i++){
			Element personElement = (Element) nodes.item(i);
			Person person = new Person();
			person.setId(new Integer(personElement.getAttribute("id")));//获得person属性值
			NodeList childnodes = personElement.getChildNodes();
			for(int y = 0 ; y < childnodes.getLength() ; y++){
				Node childNode = (Node)childnodes.item(y);
				if(childNode.getNodeType()==Node.ELEMENT_NODE){//判断类型是不是element类型的节点(因为当中还有文本节点)
					Element childElement = (Element) childNode;
					if("name".equals(childElement.getNodeName())){
						person.setName(childElement.getFirstChild().getNodeValue());
					} else if("age".equals(childElement.getNodeName())){//如果age可以为空,则还需要进行判断
						person.setAge(new Short(childElement.getFirstChild().getNodeValue()));
					}
				} else {
					//不是正常的element节点;有可能是文本节点
				}
			}
			persons.add(person);
		}
		return persons;
	}
}

你可能感兴趣的:(java,xml,android,JUnit)