Book. xml
- <? Xml version = "1.0" encoding = "UTF-8"?>
-
- <Bookshelves>
- <Book>
- <Title> Basic Java employment tutorial </title>
- <Author> Zhang Xiaoxiang </author>
- <Price> 48 RMB </price>
- <Price> 58 RMB </price>
- </Book>
- <Book>
- <Title> JavaScript web development </title>
- <Author> Li fuming </author>
- <Price> 46 RMB </price>
- </Book>
- </Bookshelves>
Dom4j. java
- Package yyy. dom4j;
-
- Import java. io. File;
- Import java. io. FileOutputStream;
- Import java. io. OutputStreamWriter;
- Import java. util. List;
-
- Import org. dom4j. Document;
- Import org. dom4j. DocumentHelper;
- Import org. dom4j. Element;
- Import org. dom4j. Node;
- Import org. dom4j. io. OutputFormat;
- Import org. dom4j. io. SAXReader;
- Import org. dom4j. io. XMLWriter;
- Import org. junit. Test;
-
-
- Public class Dom4j {
- Private File file = new File ("src/book. xml ");
- // Traverse the xml document
- @ Test
- Public void testList () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element root = document. getRootElement ();
- List (root );
- }
- Public void list (Element element ){
- System. out. println (element. getName ());
- List <Node> nodes = element. elements ();
- // System. out. println (nodes. size ());
- For (Node node: nodes ){
- If (node instanceof Element ){
- List (Element) node );
- }
- }
- }
- // Read the value of a node
- @ Test
- Public void read () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element root = document. getRootElement ();
- Element price = root. element ("book"). element ("author ");
- String value = price. getText ();
- System. out. println (value );
- }
- // Add a node
- @ Test
- Public void add () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element price = incluenthelper. createElement ("price"); // create a node
- Price. setText ("44 yuan"); // set the node Value
- Element root = document. getRootElement ();
- Root. element ("book"). add (price); // add a node
- // Re-write the document object in the memory into the xml document. Pay special attention to garbled characters.
-
- // Method 1: Use OutputStreamWriter to set the encoding table used for writing a document
- OutputStreamWriter out = new OutputStreamWriter (new FileOutputStream (file), "UTF-8 ");
- Document. write (out );
- Out. close ();
- /* // Method 2:
- OutputFormat format = OutputFormat. createPrettyPrint ();
- Format. setEncoding ("gb2312 ");
- XMLWriter writer = new XMLWriter (new FileWriter (file), format );
- Writer. write (document );
- Writer. close ();*/
- /* // Method 3:
- OutputFormat format = OutputFormat. createPrettyPrint ();
- XMLWriter writer = new XMLWriter (new OutputStreamWriter (new FileOutputStream (file), "UTF-8"), format );
- Writer. write (document );
- Writer. close ();*/
- /* // Method 4:
- OutputFormat format = OutputFormat. createPrettyPrint ();
- 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.
- XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
- Writer. write (document );
- Writer. close ();*/
- // 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
- * Garbled problem summary:
- * 1. garbled characters are required to write data into character flow files, and garbled characters are not required for throttling.
- * Write data into a character flow file using the local code table "gb2312" by default"
- * 2. Any object is read into memory in the form of "UTF-8" Encoding
- * By default, the write method of XMLWriter sends the document Object in memory to FileWriter in the form of "UTF-8" encoding,
- * 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.
- * The encoding format used for stream objects.
- **/
- }
- // Add a node to a specified position
- @ Test
- Public void add2 () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element root = document. getRootElement ();
- Element price = incluenthelper. createElement ("price ");
- Price. setText ("48 yuan ");
- List list = root. element ("book"). elements ();
- List. add (2, price );
- OutputFormat format = OutputFormat. createPrettyPrint ();
- XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
- Writer. write (document );
- Writer. close ();
- }
- // Delete a node
- @ Test
- Public void delete () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element root = document. getRootElement ();
- Element price = root. element ("book"). element ("price ");
- Price. getParent (). remove (price );
- OutputFormat format = OutputFormat. createPrettyPrint ();
- XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
- Writer. write (document );
- Writer. close ();
- }
- // Modify the content of a node
- @ Test
- Public void update () throws Exception {
- SAXReader reader = new SAXReader ();
- Document document = reader. read (file );
- Element root = document. getRootElement ();
- Element price = root. element ("book"). element ("price ");
- Price. setText ("58 RMB ");
- OutputFormat format = OutputFormat. createPrettyPrint ();
- XMLWriter writer = new XMLWriter (new FileOutputStream (file), format );
- Writer. write (document );
- Writer. close ();
- }
- }