dom解析xml范例一

dom解析xml实例一

 解析xml并把xml中用户信息提取出来

 

1 test-b.xml文件

<?xml version="1.0" encoding="UTF-8"?>
	<result>
			<man>
				<name>sky</name>
				<age>25</age>
				<sex>男</sex>
				<demo>人皇</demo>
			</man>
			<man>
				<name>fly</name>
				<age>23</age>
				<sex>男</sex>
				<demo>中国小兽皇</demo>
			</man>
			<woman>
				<name>小仓美眉</name>
				<age>20</age>
				<sex>女</sex>
				<demo>魔兽女子第一人</demo>
			</woman>
			<woman>
				<name>doudou</name>
				<age>20</age>
				<sex>女</sex>
				<demo>美女主持啊</demo>
			</woman>
	</result>

 2 用户信息People类

package com.java.xml.dom;

	public class People {
		
		private String name;
		
		private String age;
		
		private String sex;
		
		private String demo;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getAge() {
			return age;
		}

		public void setAge(String age) {
			this.age = age;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

		public String getDemo() {
			return demo;
		}

		public void setDemo(String demo) {
			this.demo = demo;
		}
	}

 3 java解析 执行方法DomB

package com.java.xml.dom;

	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.Node;
	import org.w3c.dom.NodeList;

	public class DomB {

		/**
		 * 根据节点属性给用户赋值
		 * @param node
		 * @param people
		 */
		public static void getUserPara(Node node,People people)
		{
			//节点名称
			String name = node.getNodeName();
			//节点值
			String value = node.getFirstChild().getNodeValue();
			
			if ("name".equals(name))
			{
				people.setName(value);
			}
			
			if ("age".equals(name))
			{
				people.setAge(value);
			}
			
			if ("sex".equals(name))
			{
				people.setSex(value);
			}
			
			if ("demo".equals(name))
			{
				people.setDemo(value);
			}
		}
		
		/**
		 * 输出到控制台
		 * @param i
		 * @param people
		 */
		public static void print(int i,People people)
		{
			StringBuffer sb = new StringBuffer(100);
			sb.append(i+1).append(":姓名 ").append(people.getName())
			.append(" 年龄 ").append(people.getAge()).append(" 性别 ")
			.append(people.getSex()).append(" 备注 ").append(people.getDemo());
			System.out.println(sb.toString());
		}
		/**
		 * dom解析"
		 */
		public static void dom()
		{
			try 
			{
				//用户对象集
				List<People> userList = new ArrayList<People>(16);
				
				File xml = new File("D:/新的开始/学习笔记/java学习/xml解析/test-b.xml");
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document doc = builder.parse(xml);
				
				//doc根对象	
				Node root = doc.getDocumentElement();
				Node node1 = null;
				Node node2 = null;
				
				//根节点子对象集合
				NodeList list = root.getChildNodes();
				NodeList list1 = null;
				
				//开始解析处理xml节点,本来想写个灵活点的解析处理方法的
				//后来发现没有这个必要,所以下面的代码都是基于知道xml结构的情况下解析的。
				for (int i = 0; i < list.getLength(); i++)
				{
					//此处对象是man/woman对象
					node1 = list.item(i);
					
					//如果是对象节点继续处理,空白文本节点不住处理
					if (Node.ELEMENT_NODE == node1.getNodeType())
					{
						People people = new People();
						
						list1 = node1.getChildNodes();
						
						for (int j = 0; j < list1.getLength(); j++)
						{
							//此处对象是user属性对象,即name/age/sex/demo
							node2 = list1.item(j);
							
							//如果是对象节点继续处理,空白文本节点不住处理
							if (Node.ELEMENT_NODE == node2.getNodeType())
							{	
								//根据xml值给people对象注入属性
								getUserPara(node2,people);	
							}
						}
						
						//添加用户对象到集合中
						userList.add(people);
					}
				}
				
				
				People people = null;
				
				//控制台输p出
				for (int i = 0; i< userList.size(); i++)
				{
					people = userList.get(i);
					print(i,people);
				}
				
		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		/**
		 * @param args
		 */
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			dom();
		}

	}

 4 执行DomB对象的main方法,输出如下:

1:姓名 sky 年龄 25 性别 男 备注 人皇
2:姓名 fly 年龄 23 性别 男 备注 中国小兽皇
3:姓名 小仓美眉 年龄 20 性别 女 备注 魔兽女子第一人
4:姓名 doudou 年龄 20 性别 女 备注 美女主持啊