XML文档,DOM解析

 DOM解析模型

<?xml version="1.0" encoding="UTF-8"?>
<memo>
	<note type="birthday">
		<name>张三</name>
		<sex>男</sex>
		<date>1984</date>
	</note>
		<note type="shop">
		<name>李四</name>
		<sex>男</sex>
		<date>1984</date>
	</note>
	<note type="go">
		<name>王五</name>
		<sex>男</sex>
		<date>1984</date>
	</note>
</memo>

 

package domTest;

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

public class DomTest {
	public static void main(String[] args)  {
		File xmlFile = new File("C:"+File.separator+"Users"+File.separator+"357"+File.separator+"Downloads"+File.separator+"memo.xml");//C:\Users\357\Downloads
		DocumentBuilder builder = null;
		
		//声明一个DocumentBuilderFactory对象,通过单例模式创建
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		try {
			builder = documentBuilderFactory.newDocumentBuilder();//取得默认的DocumentBuilder
			Document document = builder.parse(xmlFile);//解析文件
			Element root= document.getDocumentElement();//获取根元素
			NodeList childNodes = root.getChildNodes();//获得根元素下的子节点
			for (int i = 0; i < childNodes.getLength(); i++) {//对每个子节点进行判断
				Node node = childNodes.item(i);
				if("note".equals(node.getNodeName())){//如果节点的名称为"note",则输出note元素熟悉type
					System.out.println("\r\n 找到一篇日记,所属分类;"+node.getAttributes().getNamedItem("type").getNodeValue()+".");
					NodeList nodeDetail = node.getChildNodes(); //获得<memo>下的节点
					for (int j = 0; j < nodeDetail.getLength(); j++) {
						//遍历<memo>下的节点
						Node detail = nodeDetail.item(j);
						switch (detail.getNodeName()) {
						case "name":
							System.out.println("姓名:"+detail.getTextContent());
							break;
						case "sex":
							System.out.println("性别:"+detail.getTextContent());
							break;
						case "date":
							System.out.println("生日:"+detail.getTextContent());
							break;

						default:
							break;
						}
						
					}
						 
				}
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
			
			
			
			
	
	}
}

3个步骤创建XML文件对应的Document对象:

1.获取factory,代码如下:

    //声明一个DocumentBuilderFactory对象,通过单例模式创建
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

2.获取builder,代码如下:

    DocumentBuilder   builder = documentBuilderFactory.newD ocumentBuilder();//取得默认的DocumentBuilder

3.解析为Document对象, 代码如下:

    Document document = builder.parse(xmlFile);//解析文件

 

XML有哪些解析技术?区别是上面?

    有DOM,SAX,STAX等

    DOM:处理大型文件时 性能下降的非常厉害,DOM的树结构暂用的内存较多,而且DOM必须在解析文件之前把整个文档装入内存。

    SAX:事件驱动的XML解析方式。顺序读取XML文件, 

 

转载于:https://my.oschina.net/fivewang/blog/753502

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值