xml增删改查

package com.cgm.crudxml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CrudXmlTest {

public static void main(String[] args) {

Document document = getDocument();

xmlAddElement(document);

updateXml(document); //修改xml文档对象
SaveDocumentXml(document); //保存xml文档对象

deleteXmlNode(document); //删除xml节点对象

}

private static void xmlAddElement(Document document) {
//增加节点
Element rootElemt = document.getDocumentElement(); //取得根节点
Element newElement = document.createElement("book"); //创建新元素
newElement.setAttribute("id", "b008");

Element tiElement = document.createElement("title");
newElement.appendChild(tiElement);
tiElement.setTextContent("天龙八部");

Element priceElement = document.createElement("price");
newElement.appendChild(priceElement);
priceElement.setTextContent("200");

rootElemt.appendChild(newElement); //追加新节点
SaveDocumentXml(document); //保存xml文档对象
}

private static void deleteXmlNode(Document document) {

try {
NodeList nodeList = document.getElementsByTagName("book");

for (int i = 0; i < nodeList.getLength(); i++) {
Node bookNode = nodeList.item(i);
Element bookElement = (Element) bookNode;
String id = bookElement.getAttribute("id").trim(); //取得book的Id名称

System.out.println("id的只是===========" + id);
if ("b003".equals(id)) { //删除书籍

Node parentNode = bookElement.getParentNode();
Node node = parentNode.removeChild(bookElement);
System.out.println("===" + node);

}

}

SaveDocumentXml(document);
} catch (Exception e) {
e.printStackTrace();
}

}

private static void updateXml(Document document) {
try {
NodeList nodeList = document.getElementsByTagName("book");

for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element element = (Element) node;
String id = element.getAttribute("id"); //取得book的Id名称
if ("b001".equals(id)) { //找到书籍
NodeList chiList = element.getElementsByTagName("title");
Node title = chiList.item(0);

System.out.println(title.getTextContent());

//Node price = title.getNextSibling();

// System.out.println(price.getTextContent());
//price.setTextContent("100000");

title.setTextContent("C++基础");

}

}
} catch (Exception e) {
e.printStackTrace();
}

}

/*
* 保存xml
*/

private static void SaveDocumentXml(Document document) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Source xmlsSource = new DOMSource(document);
Result result = new StreamResult("books.jasp.xml");

transformer.transform(xmlsSource, result);
System.out.println("done");

} catch (Exception e) {
e.printStackTrace();
}
}

/*
* 取得xml
*/
public static Document getDocument() {
Document document = null;
try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse("book.xml");
System.out.println(document);
NodeList boolNodeList = document.getElementsByTagName("book");
System.out.println(boolNodeList);
} catch (Exception e) {
e.printStackTrace();
}

return document;
}

}

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="b001">
<title>java core</title>
<price>688</price>
</book>
<book id="b002">
<title>思想政治</title>
<price>515</price>
</book>
<book id="b003">
<title>世界历史</title>
<price>545</price>
</book>
<book id="b004">
<title>大学生物</title>
<price>512</price>
</book>
</books>

你可能感兴趣的:(增删改查)