采用Dom实现Xml 文档的增加,删除,修改,查询
- xml version="1.0" encoding="GB2312" standalone="no"?>
- <books>
- <book id="1">
- <name>数据结构name>
- <author>严蔚敏author>
- book>
- books>
实现代码:
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.OutputKeys;
- 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.NodeList;
- public class DomTest {
- public static void main(String[] args) throws Exception {
- init();
- add();
- traverse();
- edit(1);
- del(2);
- }
- private static void del(int id) throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(new File("books.xml"));
- Element root = document.getDocumentElement();
- NodeList list = root.getChildNodes();
- for (int i = 0; i < list.getLength(); i++) {
- Element e = (Element) list.item(i);
- if (Integer.parseInt(e.getAttribute("id")) == id) {
- root.removeChild(e);
- break;
- }
- }
- TransformerFactory tsf = TransformerFactory.newInstance();
- Transformer tf = tsf.newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "GB2312");
- tf.setOutputProperty(OutputKeys.INDENT, " ");
- StreamResult result = new StreamResult(new File("books.xml"));
- DOMSource source = new DOMSource(document);
- tf.transform(source, result);
- }
- private static void edit(int id) throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(new File("books.xml"));
- Element root = document.getDocumentElement();
- NodeList list = root.getChildNodes();
- for (int i = 0; i < list.getLength(); i++) {
- Element e = (Element) list.item(i);
- if (Integer.parseInt(e.getAttribute("id")) == id) {
- e.getElementsByTagName("name").item(0).setTextContent("数据结构");
- e.getElementsByTagName("author").item(0).setTextContent("严蔚敏");
- break;
- }
- }
- TransformerFactory tsf = TransformerFactory.newInstance();
- Transformer tf = tsf.newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "GB2312");
- tf.setOutputProperty(OutputKeys.INDENT, " ");
- StreamResult result = new StreamResult(new File("books.xml"));
- DOMSource source = new DOMSource(document);
- tf.transform(source, result);
- }
- private static void traverse() throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(new File("books.xml"));
- Element root = document.getDocumentElement();
- NodeList list = root.getChildNodes();
- for (int i = 0; i < list.getLength(); i++) {
- Element e = (Element) list.item(i);
- System.out.println("id: " + e.getAttribute("id"));
- System.out.println("书籍名称:"
- + e.getElementsByTagName("name").item(0).getTextContent());
- System.out
- .println("书籍作者 :"
- + e.getElementsByTagName("author").item(0)
- .getTextContent());
- }
- }
- private static void add() throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(new File("books.xml"));
- Element root = document.getDocumentElement();
- Element parent = document.createElement("book");
- Element name = document.createElement("name");
- Element author = document.createElement("author");
- parent.setAttribute("id", "2");
- name.appendChild(document.createTextNode("操作系统"));
- author.setTextContent("汤子赢");
- parent.appendChild(name);
- parent.appendChild(author);
- root.appendChild(parent);
- TransformerFactory tsf = TransformerFactory.newInstance();
- DOMSource source = new DOMSource(document);
- Transformer tf = tsf.newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "GB2312");
- tf.setOutputProperty(OutputKeys.INDENT, " ");
- StreamResult result = new StreamResult(new File("books.xml"));
- tf.transform(source, result);
- }
- private static void init() throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.newDocument();
- Element root = document.createElement("books");
- Element parent = document.createElement("book");
- Element name = document.createElement("name");
- Element author = document.createElement("author");
- parent.setAttribute("id", "1");
- name.appendChild(document.createTextNode("计算机网络"));
- author.setTextContent("谢希仁");
- parent.appendChild(name);
- parent.appendChild(author);
- root.appendChild(parent);
- document.appendChild(root);
- TransformerFactory tsf = TransformerFactory.newInstance();
- DOMSource source = new DOMSource(document);
- Transformer tf = tsf.newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "GB2312");
- tf.setOutputProperty(OutputKeys.INDENT, " ");
- StreamResult result = new StreamResult(new File("books.xml"));
- tf.transform(source, result);
- }
- }
结果: