Xml语法分析

前端之家收集整理的这篇文章主要介绍了Xml语法分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

解析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文档的增删改查:

  1. package Xml;
  2.  
  3. import java.io.FileOutputStream;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.transform.Transformer;
  8. import javax.xml.transform.TransformerFactory;
  9. import javax.xml.transform.dom.DOMSource;
  10. import javax.xml.transform.stream.StreamResult;
  11.  
  12. import org.junit.Test;
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.Element;
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17.  
  18.  
  19. public class Demo1 {
  20.  
  21. @Test
  22. public void read1() throws Exception{
  23. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  24. DocumentBuilder builder = factory.newDocumentBuilder();
  25. Document document = builder.parse("src/Xml/MyXml.xml");
  26. NodeList list = document.getElementsByTagName("书名");
  27. Node node = list.item(1);
  28. String content = node.getTextContent();
  29. System.out.println(content);
  30. }
  31. @Test
  32. public void read2() throws Exception{
  33. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  34. DocumentBuilder builder = factory.newDocumentBuilder();
  35. Document document = builder.parse("src/Xml/MyXml.xml");
  36. Node root = document.getElementsByTagName("书架").item(0);
  37. list(root);
  38. }
  39.  
  40. private void list(Node root) {
  41. if(root instanceof Element){
  42. System.out.println(root.getNodeName());
  43. }
  44. // System.out.println(root.getNodeName());
  45. NodeList list = root.getChildNodes();
  46. for(int i=0;i<list.getLength();i++){
  47. Node child = list.item(i);
  48. list(child);//递归了
  49. }
  50. }
  51. @Test
  52. public void read3() throws Exception{
  53. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  54. DocumentBuilder builder = factory.newDocumentBuilder();
  55. Document document = builder.parse("src/Xml/MyXml.xml");
  56. Element root = (Element) document.getElementsByTagName("书名").item(0);
  57. String bookname = root.getAttribute("name");
  58. System.out.println(bookname);
  59. }
  60. //增
  61. @Test
  62. public void add() throws Exception{
  63. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  64. DocumentBuilder builder = factory.newDocumentBuilder();
  65. Document document = builder.parse("src/Xml/MyXml.xml");
  66. Element price = document.createElement("售价");
  67. price.setTextContent("59.00元");
  68. //把结点挂在"书"结点下
  69. Element book = (Element) document.getElementsByTagName("书").item(0);
  70. book.appendChild(price);
  71. TransformerFactory ttfactory = TransformerFactory.newInstance();
  72. Transformer tf = ttfactory.newTransformer();
  73. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  74. }
  75. @Test
  76. public void add1() throws Exception{
  77. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  78. DocumentBuilder builder = factory.newDocumentBuilder();
  79. Document document = builder.parse("src/Xml/MyXml.xml");
  80. //得到创建结点
  81. Element price = document.createElement("售价");
  82. price.setTextContent("59.00元");
  83. Element refNode = (Element) document.getElementsByTagName("售价").item(0);
  84. //把结点挂在"书"结点下
  85. Element book = (Element) document.getElementsByTagName("书").item(0);
  86. book.insertBefore(price,refNode);
  87. //把更改后的文件写入内存
  88. TransformerFactory ttfactory = TransformerFactory.newInstance();
  89. Transformer tf = ttfactory.newTransformer();
  90. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  91. }
  92. @Test
  93. public void addAttr() throws Exception{
  94. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  95. DocumentBuilder builder = factory.newDocumentBuilder();
  96. Document document = builder.parse("src/Xml/MyXml.xml");
  97. Element bookname = (Element) document.getElementsByTagName("书名").item(0);
  98. bookname.setAttribute("name","xxx");
  99. //把更改后的文件写入内存
  100. TransformerFactory ttfactory = TransformerFactory.newInstance();
  101. Transformer tf = ttfactory.newTransformer();
  102. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  103. }
  104. //删
  105. @Test
  106. public void delte() throws Exception{
  107. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  108. DocumentBuilder builder = factory.newDocumentBuilder();
  109. Document document = builder.parse("src/Xml/MyXml.xml");
  110. Element book = (Element) document.getElementsByTagName("书").item(0);
  111. Element price = (Element) document.getElementsByTagName("售价").item(0);
  112. book.removeChild(price);
  113. //把更改后的文件写入内存
  114. TransformerFactory ttfactory = TransformerFactory.newInstance();
  115. Transformer tf = ttfactory.newTransformer();
  116. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  117. }
  118. @Test
  119. public void delte1() throws Exception{
  120. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  121. DocumentBuilder builder = factory.newDocumentBuilder();
  122. Document document = builder.parse("src/Xml/MyXml.xml");
  123. Element e = (Element) document.getElementsByTagName("售价").item(0);
  124. //删除所在结点所在的书
  125. e.getParentNode().getParentNode().removeChild(e.getParentNode());
  126. //把更改后的文件写入内存
  127. TransformerFactory ttfactory = TransformerFactory.newInstance();
  128. Transformer tf = ttfactory.newTransformer();
  129. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  130. }
  131. @Test
  132. public void update() throws Exception{
  133. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  134. DocumentBuilder builder = factory.newDocumentBuilder();
  135. Document document = builder.parse("src/Xml/MyXml.xml");
  136. Element e = (Element) document.getElementsByTagName("售价").item(0);
  137. e.setTextContent("109.00元");
  138. //把更改后的文件写入内存
  139. TransformerFactory ttfactory = TransformerFactory.newInstance();
  140. Transformer tf = ttfactory.newTransformer();
  141. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/Xml/MyXml.xml")));
  142. }
  143. }


  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <书架>
  3. <书>
  4. <书名 name="xxx">JavaWeb开发教程</书名>
  5. <作者>张孝祥</作者>
  6. <售价>59</售价>
  7. <售价>34</售价>
  8. </书>
  9. <书>
  10. <书名>JavaScript开发教程</书名>
  11. <作者>黎活明</作者>
  12. <售价>25</售价>
  13. </书>
  14. </书架>

猜你在找的XML相关文章