DOM解析

   DOM解析

     DOM :Document Object Model (文档对象模型)

     对于XML应用开发来说,DOM就是一个对象化的XML数据接口,一个与语言无关,与平台无关的标准接口规范。

     小重点:根结点代表的是XML文档本身程序的入口。

                     根元素结点则表示XML文档的根元素(ROOT)

     简单的例子:

     XML

<?xml version="1.0" encoding="utf-8"?>
<学生名册>
<!-- this is my comment -->
  <学生 学号="1">
     <姓名>张三</姓名>
     <性别>男</性别>
     <年龄>19</年龄>
  </学生>
  <学生 学号="2">
     <姓名>李四</姓名>
     <性别>男</性别>
     <年龄>20</年龄>
  </学生>
</学生名册>

    XMLTest:

 

package com.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

public class XMLTest
{
	public static void main(String[] args) throws Exception
	{
		//获取dom解析器工厂
		DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
		
		//获取具体的dom解析器
		DocumentBuilder db = dbf.newDocumentBuilder();
		
		//解析具体的XML文件
		Document document = db.parse(new File("third.xml"));
		
		NodeList nl = document.getElementsByTagName("学生");
		
		//System.out.println(nl.getLength());
		
		for(int i=0;i<nl.getLength();i++)
		{
			Element element =(Element)nl.item(i);
			
			String context1 = element.getElementsByTagName("姓名").item(0).getFirstChild().getNodeValue();
			
			System.out.println(context1);
			
            String context2 = element.getElementsByTagName("性别").item(0).getFirstChild().getNodeValue();
			
			System.out.println(context2);
			
            String context3 = element.getElementsByTagName("年龄").item(0).getFirstChild().getNodeValue();
			
			System.out.println(context3);
			
			//获取属性
			NamedNodeMap nnm = nl.item(i).getAttributes();
			
			String attrName = nnm.item(0).getNodeName();
			
			System.out.println(attrName);
			
			String attrValue = nnm.item(0).getNodeValue();
			
			System.out.println(attrValue);
		}
	}
}

  注意:xml要放在lib目录下,这样可以直接相对根目录找到"third.xml",不然要加相对地址。

  利用递归读取third.xml内容:

package com.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * 解析XML文件
 */
public class DOMTest
{
	public static void main(String[] args) throws Exception
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document document = db.parse(new File("third.xml"));
		
		//根元素
		Element root = document.getDocumentElement();
		parseElement(root);
	}
	
	public static void parseElement(Element element)
	{
		String tarName = element.getNodeName();
		
		System.out.print("<"+tarName);
		   
		NodeList children = element.getChildNodes();
		
		NamedNodeMap map = element.getAttributes();
		
		if(null!=map)
		{
		   for(int i =0; i<map.getLength();i++)
		   {
			   Attr attr =(Attr) map.item(i);
			   
			   String attrName = attr.getName();
			   String attrValue= attr.getValue();
			   
			   System.out.print(" "+attrName+"=\""+attrValue+"\"");
		   }
		}
		System.out.print(">");
		
		for(int i =0;i<children.getLength();i++)
		{
			Node node = children.item(i);
			
			//获取结点的类型,可能为孩子,也可能为注释
			short nodeType = node.getNodeType();
			
			if(nodeType == Node.ELEMENT_NODE)
			{
				//递归
				parseElement((Element)node);
			}
			else if (nodeType == Node.TEXT_NODE)
			{
				System.out.print(node.getNodeValue());
			}
			else if (nodeType == Node.COMMENT_NODE)
			{
				//注释内容 
				Comment comment = (Comment)node;
				
				String data = comment.getData();
				
				System.out.println("<!--"+data+"-->");
			}
		}
		System.out.println("</"+tarName+">");
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值