欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DOM4J

程序员文章站 2022-05-29 11:29:43
...
public class DOM_Person {

	public static List<Person> getPersons(InputStream inStream)
			throws Throwable {
		List<Person> persons = new ArrayList<Person>();
		/* 实例化一个文档构建器工厂 */
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		/* 通过文档构建器工厂获取一个文档构建器 */
		DocumentBuilder builder = factory.newDocumentBuilder();
		/* 通过文档构建器构建一个文档实例 */
		Document documnet = builder.parse(inStream);// 调用parse()完成解析xml文件,形成文档树

		Element root = documnet.getDocumentElement();// 取得根元素

		NodeList personNodes = root.getElementsByTagName("person");

		for (int i = 0; i < personNodes.getLength(); i++) {
			Person person = new Person();
			Element personElement = (Element) personNodes.item(i);
			person.setId(new Integer(personElement.getAttribute("id")));
			NodeList personChilds = personElement.getChildNodes();// 取得Person的子节点
			for (int y = 0; y < personChilds.getLength(); y++) {
				if (personChilds.item(y).getNodeType() == Node.ELEMENT_NODE) {// 判断当前节点是否是元素类型节点
					Element childElement = (Element) personChilds.item(y);
					if ("name".equals(childElement.getNodeName())) {
						person.setName(childElement.getFirstChild()
								.getNodeValue());// 取得第一个子节点的文本值
					} else if ("age".equals(childElement.getNodeName())) {
						person.setAge(new Short(childElement.getFirstChild()
								.getNodeValue()));
					}
				}
			}
			persons.add(person);
		}
		return persons;
	}
}

 

 

 

public class Main extends Activity {
		public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		/* 获取XML文件流 */
		InputStream inStream = getClass().getClassLoader().getResourceAsStream(
				"itcast.xml");

		List<Person> persons = null;
		/* DOM4j解析XML文件 */

		try {
			persons = DOM_Person.getPersons(inStream);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("/* DOM解析XML文件 */");
		for (Person person : persons) {
			System.out.println(person.toString());
		}
	}
}