使用DOM方式对XML进行CRUD

DOM:将xml文档解析成对象和子对象(标签对象,属性对象)

缺点:占内存,如有必要需要修改虚拟机的运行内存

优点:CRUD容易操作


示例代码提示:用DOM方式对book.xml进行增删改查

book.xml


<书架>
	<书>
		<书名 name="XXX">红楼梦
		<作者>曹雪芹
		<售价>80元
	
	
	<书>
		<书名>水浒传
		<作者>施耐庵
		<售价>60
	
	 
Demo.java

import java.io.File;

import javax.print.Doc;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 Demo3 {

	// 读取xml中标签中的值
	@Test
	public void read1() throws Exception {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/book.xml");

		NodeList list = document.getElementsByTagName("书名");
		Node node = list.item(1);
		String content = node.getTextContent();

		System.out.println(content);
	}

	// 得到XML中的所有标签
	@Test
	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);
		}
	}

	// 得到XML中的标签属性中的值
	@Test
	public void read3() throws Exception {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/book.xml");

		Element element = (Element) document.getElementsByTagName("书名").item(0);
		String bookname = element.getAttribute("name");
		System.out.println(bookname);
	}

	// 向XML文档中添加节点:<售价>59.00元
	@Test
	public void add() throws Exception {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/book.xml");

		// 创建节点
		Element element = document.createElement("售价");
		element.setTextContent("59.00元");

		// 把创建的节点挂到第一个节点上
		Element elt = (Element) document.getElementsByTagName("书").item(0);
		elt.appendChild(element);

		// 将更新后内存写会到XML文档
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
				"src/book.xml")));
	}

	// 向XML文档指定位置添加节点:<售价>59.00元
	@Test
	public void add2() throws Exception {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/book.xml");

		// 创建节点
		Element element = document.createElement("售价");
		element.setTextContent("59.00元");

		//得到参考节点
		Element refNode = (Element) document.getElementsByTagName("售价").item(0);
		
		//得到要挂崽的节点
		Element book = (Element) document.getElementsByTagName("书").item(0);
		
		//往book指定位置插入崽
		book.insertBefore(element, refNode);
		
		// 将更新后内存写会到XML文档
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
				"src/book.xml")));
	}
	
	// 向XML文档节点添加属性:<书名>红楼梦 name = "XXX"属性
		@Test
		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", "XXX");
			
			TransformerFactory tffactory = TransformerFactory.newInstance();
			Transformer tf = tffactory.newTransformer();
			tf.transform(new DOMSource(document), new StreamResult(new File(
					"src/book.xml")));
		}
		
		@Test
		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 father = (Element) document.getElementsByTagName("书").item(0);
			
			father.removeChild(e);
			
			TransformerFactory tffactory = TransformerFactory.newInstance();
			Transformer tf = tffactory.newTransformer();
			tf.transform(new DOMSource(document), new StreamResult(new File("src/book.xml")));
		}
		
		@Test
		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().getParentNode().getParentNode().removeChild(e.getParentNode().getParentNode());
			
			TransformerFactory tffactory = TransformerFactory.newInstance();
			Transformer tf = tffactory.newTransformer();
			tf.transform(new DOMSource(document), new StreamResult(new File("src/book.xml")));
		}
		
		//更新价格
		@Test
		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("110元");
			
			TransformerFactory tffactory = TransformerFactory.newInstance();
			Transformer tf = tffactory.newTransformer();
			tf.transform(new DOMSource(document), new StreamResult(new File("src/book.xml")));
		}
}



你可能感兴趣的:(javaweb)