使用JAXP对XML文档进行DOM解析

前端之家收集整理的这篇文章主要介绍了使用JAXP对XML文档进行DOM解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML解析方式分为两种:dom和sax

dom:(Document Object Model,即文档对象模型)是W3C组织推荐的解析XML的一种方式。

sax:(Simple API for XML)不是官方标准,但它是XML社区事实上的标准,几乎所有的XML解析器都支持它。


dom和sax解析方法的区别:

1.dom解析的优点:对文档CRUD比较方便;缺点:占用内存比较大

2.sax解析的优点:占用内存少,解析速度快;取点:只适合做文档读取,不适合做文档的CRUD


XML解析开发包:Jaxp(sun)、Jdom、dom4j


使用JAXP对XML文档进行DOM解析

核心代码

                //1,创建工厂
		DocumentBuilderFactory factor=DocumentBuilderFactory.newInstance();
		
		//2,得到dom解析器
		DocumentBuilder builder=factor.newDocumentBuilder();
		
		//3,解析xml文档,得到代表文档的document
		builder.parse("src/book.xml");

增,删,改 需要将更新后的内容在写到xml中
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));


下面是一些基本练习,比较基础简单,需 用JUnit Test运行

book.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<书架>
<span style="white-space:pre">	</span><书>
<span style="white-space:pre">		</span><书名 name="XXXX">Java基础</书名>
<span style="white-space:pre">		</span><进价>40元</进价>
<span style="white-space:pre">		</span><零售价>159元</零售价>
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span></书>
<span style="white-space:pre">	</span><书>
<span style="white-space:pre">		</span><书名>Java从入门到精通</书名>
<span style="white-space:pre">		</span><进价>90元</进价>
<span style="white-space:pre">		</span><零售价>30元</零售价>
<span style="white-space:pre">	</span></书>
</书架>

Demo.java

package com.yong.xml;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

//使用dom方式对xml文档进行crud
public class Demo2 {

	
	@Test
	//读取book.xml文档中:<书名>Java从入门到精通</书名>   节点中的值
	public void read1() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		NodeList list =document.getElementsByTagName("书名");
		
		//dom解析下,xml文档的每一个组成部分都会用一个对象表示,不管什么对象,
		//都是Node的子类,所以在开发中科院把获取到的任意节点都当做Node对待
		Node node=list.item(1);
		String content=node.getTextContent();
		System.out.println(content);  //打印结果:Java从入门到精通
	}
	
	
	@Test
	//得到book.xml文档中的所有标签
	public void read2() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//得到根节点
		Node root=document.getElementsByTagName("书架").item(0);
		
		list(root);	
	}
	private void list(Node node) {
		if(node instanceof Element)
		{
			System.out.println(node.getNodeName());
		}
		NodeList list=node.getChildNodes();
		for(int i=0;i<list.getLength();i++)
		{
			Node child=list.item(i);
			list(child);// 打印出各个标签:书架     书      书名    进价  零售价    书   书名  进价   零售价		
		}
	}
	
	
	@Test
	//得到book.xml中标签内的属性值    <书名 name="XXXX">Java基础</书名>
	public void read3() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		Element bookname=(Element) document.getElementsByTagName("书名").item(0);
		String value=bookname.getAttribute("name");
		System.out.println(value);  //打印结果:XXXX
	}
	
	@Test
	//向book.xml中添加    <团购>12元</团购>
	public void add() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//创建节点
		Element price=document.createElement("团购");
		price.setTextContent("12元");
		
		//把创建的节点挂到第一本书上
		Element book=(Element) document.getElementsByTagName("书").item(0);
		book.appendChild(price);
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
	
	@Test
	//向book.xml中指定位置上添加节点 
	public void add2() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//创建节点
		Element price=document.createElement("批发");
		price.setTextContent("8元");
		
		//得到参考点
		Element refNode=(Element) document.getElementsByTagName("零售价").item(0);
		
		//得到要挂崽的节点
		Element book=(Element) document.getElementsByTagName("书").item(0);
		
		//往book节点的指定位置插崽
		book.insertBefore(price,refNode);
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
	
	@Test
	//向book.xml文档节点上添加属性,   <书名 >Java基础</书名>上添加 name="XXXX"属性
	public void addAttr() throws Exception
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");		
	
		Element bookname=(Element) document.getElementsByTagName("书名").item(0);
		bookname.setAttribute("name","XXXX");
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
	
	@Test
	//删除book.xml中的指定节点方法一
	public void delete1() throws Exception 
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//得到要删除的节点
		Element e=(Element) document.getElementsByTagName("售价").item(0);
		//得到要删除的节点的爸爸
		Element book=(Element) document.getElementsByTagName("书").item(0);
		//爸爸删崽
		book.removeChild(e);
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
	
	@Test
	//删除book.xml中的指定节点方法二
	public void delete2() throws Exception 
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//得到要删除的节点
		Element e=(Element) document.getElementsByTagName("售价").item(0);
		e.getParentNode().removeChild(e);
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
	
	@Test
	//更改book.xml中节点的属性
	public void update() throws Exception 
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=factory.newDocumentBuilder();
		Document document=builder.parse("src/book.xml");
		
		//得到要修改的节点
		Element e=(Element) document.getElementsByTagName("售价").item(0);
		e.setTextContent("2201元");
		
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
}
原文链接:https://www.f2er.com/xml/297402.html

猜你在找的XML相关文章