javaweb2.2 DOM解析之增删改查

javax.xml.transform包中的Transformer类用于把代表XML文件的Document对象转换成XML格式进行输出
Transformer对象通过TransformerFactory获得
Transformer类通过transform方法完成转换操作,该方法接收一个源和一个目的地。我们可以通过:
javax.xml.transform.dom.DOMSource类来关联要转换的document对象,
用javax.xml.transform.stream.StreamResult 对象来表示数据的目的地。

XML DOM增加、修改和杀出操作都是在内存中进行,必须回写
XML元素添加:1、创建元素节点2、将元素加入到指定位置
XML元素修改:1、查询指定元素2、修改属性attribute 3、修改元素内容settextcontent()
XML元素删除:1、删除节点.getParentNode.removeChild(删除节点)
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.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DOMCURD {
	@Test
	public void testAdd() throws Exception {
		// 1、将xml文件加载到内存
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("books.xml");
		// 2、添加节点 创建节点books
		Element newbook = document.createElement("book");
		newbook.setAttribute("id", "b003");
		// 3、添加那么节点
		Element newname = document.createElement("name");
		newname.setTextContent("c++");
		// 4、添加price节点
		Element newprice = document.createElement("price");
		newprice.setTextContent("60");
		// 5、将新书属性写入
		newbook.appendChild(newname);
		newbook.appendChild(newprice);
		Element root = document.getDocumentElement();
		root.appendChild(newbook);
		// 6、回写xml
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		StreamResult result = new StreamResult("books_bak.xml");
		// DOM源
		DOMSource domSource = new DOMSource(document);
		transformer.transform(domSource, result);
	}

	@Test
	// 将Java书的价格上调2被
	public void testUpdate() throws Exception {
		// 1、将xml文件加载到内存
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("books_bak.xml");
		NodeList nodeList = document.getElementsByTagName("name");
		// 2、内存中改写数据
		for (int i = 0; i < nodeList.getLength(); i++) {
			if (nodeList.item(i).getTextContent().equals("java")) {
				Element price = (Element) nodeList.item(i).getNextSibling()
						.getNextSibling();
				price.setTextContent(Integer.parseInt(price.getTextContent())
						* 2 + "");
			}
		}
		// 3、将数据回写xml
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource domSource = new DOMSource(document);
		StreamResult result = new StreamResult("books_bak.xml");
		transformer.transform(domSource, result);
	}

	@Test
	// 将c++书删除
	public void testDelete() throws Exception {
		// 1、将xml文件加载到内存
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("books_bak.xml");
		NodeList nodeList = document.getElementsByTagName("book");
		for (int i = 0; i < nodeList.getLength(); i++) {
			if (nodeList.item(i).getChildNodes().item(0).getTextContent().equals("c++")) {
				System.out.println(nodeList.item(i));
				//nodeList.item(i).getParentNode().removeChild(nodeList.item(i));
				Element book = (Element) nodeList.item(i);
				book.getParentNode().removeChild(book);
				i--;
				
			}
		}
		//2、回写xml文件
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource domSource = new DOMSource(document);
		StreamResult result = new StreamResult("books_bak.xml");
		transformer.transform(domSource, result);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值