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);
}
}