DOM方式解析XML试例

DOM解析XML实倒


XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
	<contact id="1">
		<name>zhangsan</name>
		<sex>man</sex>
		<mobile>13880246907</mobile>
		<group>
			<id>11</id>
			<name>同事</name>
		</group>
	</contact>
	<contact id="2">
		<name>lisi</name>
		<sex>man</sex>
		<mobile>15828587260</mobile>
		<group>
			<id>21</id>
			<name>家人</name>
		</group>
	</contact>
</contacts>


DomParseXML解析XML核心代码

package com.leiht.xml.dom4xml;

import java.io.File;
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.leiht.xml.entity.Contact;
import com.leiht.xml.entity.Group;

public class DomParseXML {

	public List<Contact> parse(String fileName) {
		DocumentBuilderFactory documentBuilderFactory = null;
		DocumentBuilder documentBuilder = null;
		Document document = null;

		List<Contact> contacts = new ArrayList<Contact>();

		try {
			// documentBuilderFactory工厂类,
			documentBuilderFactory = DocumentBuilderFactory.newInstance();
			// documentBuilder:获取XMLDom实例 可以理解为获取一个XML的对象以全后续操作
			documentBuilder = documentBuilderFactory.newDocumentBuilder();
			// XML的整个文档,XML文档可以看作是一个树,document可以看成是树的根结点
			document = documentBuilder.parse(new File(fileName));
			// 这是一种便捷属性,该属性允许直接访问文档的文档元素的子节点(复制JDK文档)
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getElementsByTagName("contact");

			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				Contact contact = new Contact();
				
				//设置ID
				contact.setId(Integer.parseInt(node.getAttributes()
						.getNamedItem("id").getTextContent()));
				
				//取子结点
				NodeList childList = node.getChildNodes();
				for(int j = 0; j < childList.getLength(); j++){
					Node childItem = childList.item(j);
					String tag = childItem.getNodeName();
					if(tag.equals("name")) {
						contact.setName(childItem.getTextContent());
					}else if(tag.equals("sex")){
						contact.setSex(childItem.getTextContent());
					}else if(tag.equals("mobile")) {
						contact.setMobile(childItem.getTextContent());
					}else if(tag.equals("group")) {
						NodeList groups = childItem.getChildNodes();
						Group group = new Group();
						for(int index = 0; index < groups.getLength(); index++){
							Node groupNode = groups.item(index);
							String flag = groupNode.getNodeName();
							if(flag.equals("id")){
								group.setId(groupNode.getTextContent());
							}else if(flag.equals("name")){
								group.setName(groupNode.getTextContent());
							}
						}
						
						contact.setGroup(group);
						
					}
				}
				
				contacts.add(contact);
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return contacts;
	}
}



Test.java测试类代码:
package com.leiht.xml.view;

import java.util.List;

import com.leiht.xml.dom4xml.DomParseXML;
import com.leiht.xml.entity.Contact;

public class Test {
	public static void main(String[] args) {
		DomParseXML domParseXML = new DomParseXML();
		List<Contact> contacts = domParseXML.parse("xml/contact.xml");
		
		for(Contact contact : contacts){
			System.out.println(contact);
		}
		
	}
}

               

猜你喜欢

转载自leihongtai2010.iteye.com/blog/2253501
今日推荐