解析Xml文档:
Xml文档解析有dom解析,Sax解析,dom解析优点是可以对Xml文档进行增删改查,缺点就是内存占用比较大,而sax解析只能读Xml文档,但是占用内存小,读取快。
dom解析分为三步:
1.创建一个DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2.得到dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
3.解析Xml文档,得到代表文档的document
Document document = builder.parse(“xml文件位置”);
将生成的document的文档存入内存:
TransformerFactory ttfactory = TransformerFactory.newInstance();
Transformer tf = ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Exam.xml")));
对xml文档的增删改查:
- package Xml;
- import java.io.FileOutputStream;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- 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;
- public class Demo1 {
- @Test
- public void read1() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- NodeList list = document.getElementsByTagName("书名");
- Node node = list.item(1);
- String content = node.getTextContent();
- System.out.println(content);
- }
- @Test
- public void read2() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Node root = document.getElementsByTagName("书架").item(0);
- list(root);
- }
- private void list(Node root) {
- if(root instanceof Element){
- System.out.println(root.getNodeName());
- }
- // System.out.println(root.getNodeName());
- NodeList list = root.getChildNodes();
- for(int i=0;i<list.getLength();i++){
- Node child = list.item(i);
- list(child);//递归了
- }
- }
- @Test
- public void read3() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element root = (Element) document.getElementsByTagName("书名").item(0);
- String bookname = root.getAttribute("name");
- System.out.println(bookname);
- }
- //增
- @Test
- public void add() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element price = document.createElement("售价");
- price.setTextContent("59.00元");
- //把结点挂在"书"结点下
- Element book = (Element) document.getElementsByTagName("书").item(0);
- book.appendChild(price);
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- @Test
- public void add1() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- //得到创建结点
- Element price = document.createElement("售价");
- price.setTextContent("59.00元");
- Element refNode = (Element) document.getElementsByTagName("售价").item(0);
- //把结点挂在"书"结点下
- Element book = (Element) document.getElementsByTagName("书").item(0);
- book.insertBefore(price,refNode);
- //把更改后的文件写入内存
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- @Test
- public void addAttr() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element bookname = (Element) document.getElementsByTagName("书名").item(0);
- bookname.setAttribute("name","xxx");
- //把更改后的文件写入内存
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- //删
- @Test
- public void delte() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element book = (Element) document.getElementsByTagName("书").item(0);
- Element price = (Element) document.getElementsByTagName("售价").item(0);
- book.removeChild(price);
- //把更改后的文件写入内存
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- @Test
- public void delte1() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element e = (Element) document.getElementsByTagName("售价").item(0);
- //删除所在结点所在的书
- e.getParentNode().getParentNode().removeChild(e.getParentNode());
- //把更改后的文件写入内存
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- @Test
- public void update() throws Exception{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse("src/Xml/MyXml.xml");
- Element e = (Element) document.getElementsByTagName("售价").item(0);
- e.setTextContent("109.00元");
- //把更改后的文件写入内存
- TransformerFactory ttfactory = TransformerFactory.newInstance();
- Transformer tf = ttfactory.newTransformer();
- tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
- }
- }
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <书架>
- <书>
- <书名 name="xxx">JavaWeb开发教程</书名>
- <作者>张孝祥</作者>
- <售价>59</售价>
- <售价>34</售价>
- </书>
- <书>
- <书名>JavaScript开发教程</书名>
- <作者>黎活明</作者>
- <售价>25</售价>
- </书>
- </书架>