Dom4j error in parsing and writing XML documents

Source: Internet
Author: User

Book. xml

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2.  
  3. <Bookshelves>
  4. <Book>
  5. <Title> Basic Java employment tutorial </title>
  6. <Author> Zhang Xiaoxiang </author>
  7. <Price> 48 RMB </price>
  8. <Price> 58 RMB </price>
  9. </Book>
  10. <Book>
  11. <Title> JavaScript web development </title>
  12. <Author> Li fuming </author>
  13. <Price> 46 RMB </price>
  14. </Book>
  15. </Bookshelves>

Dom4j. java

 
 
  1. Package yyy. dom4j;
  2.  
  3. Import java. io. File;
  4. Import java. io. FileOutputStream;
  5. Import java. io. OutputStreamWriter;
  6. Import java. util. List;
  7.  
  8. Import org. dom4j. Document;
  9. Import org. dom4j. DocumentHelper;
  10. Import org. dom4j. Element;
  11. Import org. dom4j. Node;
  12. Import org. dom4j. io. OutputFormat;
  13. Import org. dom4j. io. SAXReader;
  14. Import org. dom4j. io. XMLWriter;
  15. Import org. junit. Test;
  16.  
  17.  
  18. Public class Dom4j {
  19. Private File file = new File ("src/book. xml ");
  20. // Traverse the xml document
  21. @ Test
  22. Public void testList () throws Exception {
  23. SAXReader reader = new SAXReader ();
  24. Document document = reader. read (file );
  25. Element root = document. getRootElement ();
  26. List (root );
  27. }
  28. Public void list (Element element ){
  29. System. out. println (element. getName ());
  30. List <Node> nodes = element. elements ();
  31. // System. out. println (nodes. size ());
  32. For (Node node: nodes ){
  33. If (node instanceof Element ){
  34. List (Element) node );
  35. }
  36. }
  37. }
  38. // Read the value of a node
  39. @ Test
  40. Public void read () throws Exception {
  41. SAXReader reader = new SAXReader ();
  42. Document document = reader. read (file );
  43. Element root = document. getRootElement ();
  44. Element price = root. element ("book"). element ("author ");
  45. String value = price. getText ();
  46. System. out. println (value );
  47. }
  48. // Add a node
  49. @ Test
  50. Public void add () throws Exception {
  51. SAXReader reader = new SAXReader ();
  52. Document document = reader. read (file );
  53. Element price = incluenthelper. createElement ("price"); // create a node
  54. Price. setText ("44 yuan"); // set the node Value
  55. Element root = document. getRootElement ();
  56. Root. element ("book"). add (price); // add a node
  57. // Re-write the document object in the memory into the xml document. Pay special attention to garbled characters.
  58.  
  59. // Method 1: Use OutputStreamWriter to set the encoding table used for writing a document
  60. OutputStreamWriter out = new OutputStreamWriter (new FileOutputStream (file), "UTF-8 ");
  61. Document. write (out );
  62. Out. close ();
  63. /* // Method 2:
  64. OutputFormat format = OutputFormat. createPrettyPrint ();
  65. Format. setEncoding ("gb2312 ");
  66. XMLWriter writer = new XMLWriter (new FileWriter (file), format );
  67. Writer. write (document );
  68. Writer. close ();*/
  69. /* // Method 3:
  70. OutputFormat format = OutputFormat. createPrettyPrint ();
  71. XMLWriter writer = new XMLWriter (new OutputStreamWriter (new FileOutputStream (file), "UTF-8"), format );
  72. Writer. write (document );
  73. Writer. close ();*/
  74. /* // Method 4:
  75. OutputFormat format = OutputFormat. createPrettyPrint ();
  76. Format. setEncoding ("gb2312"); // the source document uses "gb2312" encoding. This does not change the encoding format of the source document, nor does it contain garbled characters.
  77. XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
  78. Writer. write (document );
  79. Writer. close ();*/
  80. // Document objects exist in the memory in the form of "UTF-8" encoding, using FileWriter to write the document object in the form of a forward stream into the xml document by default, the local code table "gb2312" Encoding
  81. * Garbled problem summary:
  82. * 1. garbled characters are required to write data into character flow files, and garbled characters are not required for throttling.
  83. * Write data into a character flow file using the local code table "gb2312" by default"
  84. * 2. Any object is read into memory in the form of "UTF-8" Encoding
  85. * By default, the write method of XMLWriter sends the document Object in memory to FileWriter in the form of "UTF-8" encoding,
  86. * You must use the encapsulated stream OutputStreamWriter to specify the encoding table used when writing the file, or use the setEncoding method of OutputFormat to specify the data to be transmitted.
  87. * The encoding format used for stream objects.
  88. **/
  89. }
  90. // Add a node to a specified position
  91. @ Test
  92. Public void add2 () throws Exception {
  93. SAXReader reader = new SAXReader ();
  94. Document document = reader. read (file );
  95. Element root = document. getRootElement ();
  96. Element price = incluenthelper. createElement ("price ");
  97. Price. setText ("48 yuan ");
  98. List list = root. element ("book"). elements ();
  99. List. add (2, price );
  100. OutputFormat format = OutputFormat. createPrettyPrint ();
  101. XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
  102. Writer. write (document );
  103. Writer. close ();
  104. }
  105. // Delete a node
  106. @ Test
  107. Public void delete () throws Exception {
  108. SAXReader reader = new SAXReader ();
  109. Document document = reader. read (file );
  110. Element root = document. getRootElement ();
  111. Element price = root. element ("book"). element ("price ");
  112. Price. getParent (). remove (price );
  113. OutputFormat format = OutputFormat. createPrettyPrint ();
  114. XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
  115. Writer. write (document );
  116. Writer. close ();
  117. }
  118. // Modify the content of a node
  119. @ Test
  120. Public void update () throws Exception {
  121. SAXReader reader = new SAXReader ();
  122. Document document = reader. read (file );
  123. Element root = document. getRootElement ();
  124. Element price = root. element ("book"). element ("price ");
  125. Price. setText ("58 RMB ");
  126. OutputFormat format = OutputFormat. createPrettyPrint ();
  127. XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
  128. Writer. write (document );
  129. Writer. close ();
  130. }
  131. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.