DOM解析XML的增删改查实现

   前面的XML读取算是已经完成了,现在是使用DOM对他进行增删改查,这个才是DOM的优势所在,其实代码很简单,不需要过多的解释,下面代码奉上

   

package com.bird.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.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @use 对XML文档进行插入删除操作
 * @author Bird
 *
 */
public class XmlChange {
	public Document getDocument() throws Exception{//返回XML文档的Document对象
		//1创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		//2.得到解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		
		//3.解析XML文档,获得代表文档的Document
		Document document = builder.parse("d://book.xml");
		
		return document;
	}
	
	public void add() throws Exception{//往XML文档中插入节点
		Document document = getDocument();
		
		//创建节点
		Element price = document.createElement("售价");
		price.setTextContent("99.00元");
		
		//把创建的节点挂到第一本书上
		Element book = (Element) document.getElementsByTagName("书").item(0);
		book.appendChild(price);
		
		//把更新后的内存重新写到文件中
		TransformerFactory tsfactory = TransformerFactory.newInstance();
		Transformer transform = tsfactory.newTransformer();
		transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream("d://book.xml")));
	}
	
	
	public void add1() throws Exception{//往XML文档中指定节点处插入节点
		Document document = getDocument();
		
		//创建节点
		Element price = document.createElement("售价");
		price.setTextContent("99.00元");
		
		//得到参考节点
		Element refElement = (Element) document.getElementsByTagName("售价").item(0);
		
		//得到要插入的主节点
		Element book = (Element) document.getElementsByTagName("书").item(0);
		
		//往book节点的指定位置插入
		book.insertBefore(price, refElement);
		
		//把更新后的内存重新写到文件中
		TransformerFactory tsfactory = TransformerFactory.newInstance();
		Transformer transform = tsfactory.newTransformer();
		transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream("d://book.xml")));
	}
	
	
	public void add2() throws Exception{//往XML文档中指定节点处更改属性
		Document document = getDocument();
		
		//得到需要插入属性的节点
		Element bookname = (Element) document.getElementsByTagName("书名").item(0);
		
		//插入属性值
		bookname.setAttribute("name", "XXXXX");
		
		
		//把更新后的内存重新写到文件中
		TransformerFactory tsfactory = TransformerFactory.newInstance();
		Transformer transform = tsfactory.newTransformer();
		transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream("d://book.xml")));
	}
	 
	public void delete() throws Exception{//删除指定节点
		Document document = getDocument();
		
		//得到要删除的节点
		Element e = (Element) document.getElementsByTagName("售价").item(0);
		
		//得到父节点后删除自己
		e.getParentNode().removeChild(e);
		
		//把更新后的内存重新写到文件中
		TransformerFactory tsfactory = TransformerFactory.newInstance();
		Transformer transform = tsfactory.newTransformer();
		transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream("d://book.xml")));
	}
	
	
	public void update() throws Exception{//更新指定节点内容
		Document document = getDocument();
		
		//得到需要更新的节点
		Element price = (Element) document.getElementsByTagName("售价").item(0);
		price.setTextContent("109元");
		
		//把更新后的内存重新写到文件中
		TransformerFactory tsfactory = TransformerFactory.newInstance();
		Transformer transform = tsfactory.newTransformer();
		transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream("d://book.xml")));
	}
	
	public static void main(String [] args) throws Exception{
		XmlChange xml = new XmlChange();
	//	xml.add();
	//	xml.add1();//往指定位置插入元素
	//	xml.add2();//往指定节点添加属性
	//	xml.delete();//删除指定的节点
		xml.update();//更新指定节点内容
	}
}

你可能感兴趣的:(xml,exception,String,delete,Class,文档)