【Project screenshot】
[person.xml] Prepare an xml file
<?xml version="1.0" encoding="UTF-8"?> <students> <student id="88888" phone="15888888888" sex="男"> <name>张三</name> <age>18</age> <school>Tsinghua</school> </student> <student id="99999" phone="15999999999" sex="女"> <name>李四</name> <age>28</age> <school>北大</school> </student> <AAAA> <aa1>aa111</aa1> <aa2>aa222</aa2> </AAAA> <BBBB></BBBB> </students>
[Read the content of the original Xml file, and then write the data to the disk]
package com.Higgin.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * Read the content data of the original xml file, and then write the data to the XML file on the disk */ public class WriteDemo01 { public static void main(String[] args) throws DocumentException, IOException { //Read the existing Xml file person.xml Document doc=new SAXReader().read(new File("./src/person.xml")); //Specify the location of the file output FileOutputStream out =new FileOutputStream("d:/student.xml"); //1. Create and write out object XMLWriter writer=new XMLWriter(out); //2. Write out the Document object writer.write(doc); //3. Close the stream writer.close(); } }
[Running result] The content is consistent with person.xml
[Save the xml content output to the corresponding disk address according to the set format]
package com.Higgin.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * 1. Read the content data of the original xml file and save it to the Document object * 2. Modify the output format (output format, encoding...) * 3. Then write the document containing the data to the XML file on disk */ public class WriteDemo02 { public static void main(String[] args) throws DocumentException, IOException { //Read the existing Xml file person.xml Document doc=new SAXReader().read(new File("./src/person.xml")); //Specify the location of the file output FileOutputStream out =new FileOutputStream("d:/student.xml"); /** * Specify the format in which the text is written: * Compact format: used when the project goes live * Pretty format: used when developing and debugging */ //OutputFormat format=OutputFormat.createCompactFormat ( ); // Compact format : remove spaces and wrap OutputFormat format =OutputFormat.createPrettyPrint ( ); // Pretty format : line break with spaces /** * Specifies that the encoding of the generated xml document affects: * 1. The encoding when the xml document is saved * 2. The encoding encoding declared by the xml document (the encoding during Xml parsing) * Conclusion: Using this method to generate Xml documents can avoid the problem of Chinese garbled characters */ format.setEncoding("UTF-8"); //1. Create and write out object XMLWriter writer=new XMLWriter(out,format); //2. Write out the Document object writer.write(doc); //3. Close the stream writer.close(); } }
【operation result】
A student.xml file is also generated under the D drive
1. Compact format:
2. Pretty formatting
3. Note:
/**
* Specifying the encoding of the generated xml document affects:
* 1. The encoding of the xml document when it is saved
* 2. The encoding encoding declared by the xml document (the encoding during Xml parsing)
* Conclusion: Using this method to generate an Xml document can avoid Chinese garbled problem
*/
[ Add, delete, modify Xml files
/**
* 1. Create a Document object,
* Add: various tags, attributes, text
* Modify: attribute values, text
* Delete: tags, attributes
* 2. Modify the output format (output format, encoding...)
* 3. Then write the document containing the data to the XML file on disk
*/
】
[Add: document, tag, attribute]
/** * Added: documents, tags, properties */ @Test public void testAdd() throws DocumentException, IOException { //1. Create a document Document doc=DocumentHelper.createDocument(); //2. Add labels Element rootElem=doc.addElement("root"); Element stuElem=rootElem.addElement("student"); stuElem.addElement("name"); //4. Add properties stuElem.addAttribute("id", "88888"); stuElem.addAttribute("sex", "男"); //Specify the location of the file output FileOutputStream out =new FileOutputStream("d:/student.xml"); // Specify the format in which the text is written: OutputFormat format=OutputFormat.createPrettyPrint(); //Pretty format: line breaks with spaces format.setEncoding("UTF-8"); //1. Create and write out object XMLWriter writer=new XMLWriter(out,format); //2. Write out the Document object writer.write(doc); //3. Close the stream writer.close(); }
【operation result】
[Modification: attribute value, text]
/** * Modify: attribute value, text */ @Test public void testUpdate() throws DocumentException, IOException{ //Create a Document object and read the existing Xml file person.xml Document doc=new SAXReader().read(new File("./src/person.xml")); /** * Modify the attribute value (Scheme 1) * Method: Use the setValue() method of the Attribute class (attribute class) * 1. Get the label object * 2. Get the attribute object * 3. Modify the attribute value */ /* //1.1 Get the label object Element stuElem=doc.getRootElement().element("student"); //1.2 Get the id attribute object Attribute idAttr=stuElem.attribute("id"); //1.3 Modify the attribute value idAttr.setValue("000001"); */ /** * Modify attribute value (Scheme 2) * Method: addAttribute("attr","value") method of Element tag class */ /* //1. Get the attribute value label Element stuElem=doc.getRootElement().element("student"); //2. Modify the attribute value by adding the method of the attribute with the same name stuElem.addAttribute ("id", " 000009 "); //key is the same, overwrite ; if there is no key, add */ /** * Modify text * method: setTest("new text value") method of Element tag class * 1. Get the label object * 2. Modify the text */ Element nameElem=doc.getRootElement().element("student").element("name"); nameElem.setText ("Wang Ermazi "); //Specify the location of the file output FileOutputStream out =new FileOutputStream("d:/student.xml"); // Specify the format in which the text is written: OutputFormat format=OutputFormat.createPrettyPrint(); //Pretty format: line breaks with spaces format.setEncoding("UTF-8"); //1. Create and write out object XMLWriter writer=new XMLWriter(out,format); //2. Write out the Document object writer.write(doc); //3. Close the stream writer.close(); }
[Delete: tags, attributes]
/** * delete: tags, attributes */ @Test public void testDelete() throws DocumentException, IOException{ //Create a Document object and read the existing Xml file person.xml Document doc=new SAXReader().read(new File("./src/person.xml")); /** * delete tag * 1. Get the label object * 2. Delete the tag object (you can delete the child tag through the parent class, or you can delete yourself) */ /* Element ageElement=doc.getRootElement().element("student").element("age"); ageElement.detach (); //Direct detch () delete //ageElement.getParent().remove(ageElement ) ; //Get the parent tag first, then remove() to delete the child tag */ /** * delete attribute */ //1. Get the label object //equivalent to Element stuElem=doc.getRootElement().element("student"); Element stuElem = (Element) doc.getRootElement (). Elements (). Get (0); //2. Get the attribute object Attribute idAttr=stuElem.attribute("id"); //3. Delete property idAttr.detach(); //Specify the location of the file output FileOutputStream out =new FileOutputStream("d:/student.xml"); // Specify the format in which the text is written: OutputFormat format=OutputFormat.createPrettyPrint(); //Pretty format: line breaks with spaces format.setEncoding("UTF-8"); //1. Create and write out object XMLWriter writer=new XMLWriter(out,format); //2. Write out the Document object writer.write(doc); //3. Close the stream writer.close(); }