Dom4j:Writing a document to a file
import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Demo1 { public static void main(String[] args) throws Exception { Document doc =new SAXReader().read("./src/contact.xml"); FileOutputStream output = new FileOutputStream(new File("F:\a.xml")); XMLWriter writer = new XMLWriter(output); writer.write(doc); output.close(); } }
//没有空格换行 OutputFormat format = new OutputFormat().createCompactFormat();//不给人看用这种,产品上线时,内容小 //有空格换行 OutputFormat format1 = new OutputFormat().createPrettyPrint();//给人看用这种,调试的时候 //(空格也是内容)
//指定xml文档的编码 format.setEncoding("utf-8");
同时影响了xml文档保存时的编码 和 xml文档声明的encoding的编码(xml解析时的编码)
结论: 使用该方法生成的xml文档避免中文乱码问题。
import java.io.File; import java.io.FileOutputStream; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.jupiter.api.Test; /* * 修改xml内容 * 增加:文档,标签 ,属性 * 修改:属性值,文本 * 删除:标签,属性 */ public class Demo3 { /* @Test public void Test() throws Exception { //创建新文档 Document doc = DocumentHelper.createDocument(); //增加标签 Element rootelem = doc.addElement("contactList"); Element contactelem = rootelem.addElement("contact"); contactelem.addElement("name"); //增加属性 contactelem.addAttribute("dsa", "454"); contactelem.addAttribute("color", "red"); //修改xml的document对象 //把修改后的Document对象写出到xml文档中 FileOutputStream output = new FileOutputStream(new File("F:\a.xml")); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(output,format); writer.write(doc); output.close(); }*/ @Test public void Test1() throws Exception { //读入文件 Document doc =new SAXReader().read("./src/contact.xml"); /** * 修改属性值 * 方法一 * 1.得到标签 * 2.得到属性对象 * 3.修改属性值 */ //方法一 /*Element contacttelem = doc.getRootElement().element("contact"); Attribute idattribute = contacttelem.attribute("id"); idattribute.setValue("4564");*/ //方法二 /*Element contacttelem = doc.getRootElement().element("contact"); //通过增加同名属性方法覆盖原来属性 contacttelem.addAttribute("id", "4564564564");*/ /** * 修改文本 * 1.得到标签 * 2.修改 */ /* Element nemeelem = doc.getRootElement().element("contact").element("name"); nemeelem.setText("李四");*/ //输出修改后的文件 OutputFormat format = OutputFormat.createPrettyPrint(); FileOutputStream output = new FileOutputStream(new File("F:\a.xml")); XMLWriter writer = new XMLWriter(output,format); writer.write(doc); output.close(); } @Test public void Test2() throws Exception { Document doc =new SAXReader().read("./src/contact.xml"); /** * 删除标签 * 得到标签。删除标签。 */ // Element ageelem = doc.getRootElement().element("contact").element("age"); //ageelem.detach(); //ageelem.getParent().remove(ageelem); /** * 删除属性 * 得到属性对象。删除属性 */ Element elem2 = (Element)doc.getRootElement().elements().get(1); Attribute attr= elem2.attribute("id"); attr.detach(); //attr.getParent().remove(attr); //输出修改后的文件 OutputFormat format = OutputFormat.createPrettyPrint(); FileOutputStream output = new FileOutputStream(new File("F:\a.xml")); XMLWriter writer = new XMLWriter(output,format); writer.write(doc); output.close(); } }
作业:
<!--使用dom4j的api来生成以下的xml文件--!> <Students> <Student id="1"> <name>张三</name> <gender>男</gender> <grade>计算机1班</grade> <address>广州天河</address> </Student> <Student id="2"> <name>李四</name> <gender>女</gender> <grade>计算机2班</grade> <address>广州越秀</address> </Student> </Students>
修改id为2的学生的姓名,改为“王丽”
删除id为2的学生
import java.io.File; import java.io.FileOutputStream; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class Demo4 { //写文件 @Test public void test() throws Exception{ Document doc = DocumentHelper.createDocument(); Element rootelem = doc.addElement("students"); Element Elems1 = rootelem.addElement("student"); Elems1.addAttribute("id", "001"); Elems1.addElement("name").setText("李杰"); Elems1.addElement("gender").setText("男"); Elems1.addElement("grade").setText("计算机科学与技术班"); Element Elems2 = rootelem.addElement("student"); Elems2.addAttribute("id", "002"); Elems2.addElement("name").setText("李英"); Elems2.addElement("gender").setText("女"); Elems2.addElement("grade").setText("计算机科学与技术班"); FileOutputStream ouput = new FileOutputStream("F:\c.xml"); OutputFormat format = new OutputFormat().createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer= new XMLWriter(ouput,format); writer.write(doc); writer.close(); } //修改id为002的学生的名字为李汉三 @Test public void test1() throws Exception{ Document doc = new SAXReader().read(new File("F:\c.xml")); Iterator<Element> it = doc.getRootElement().elementIterator("Student"); while(it.hasNext()){ Element stuElem = it.next(); if(stuElem.attributeValue("id").equals("002")){ stuElem.element("name").setText("李汉三"); break; } } FileOutputStream out = new FileOutputStream("F:\c.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out,format); writer.write(doc); writer.close(); } //删除id为002的学生的名字标签 @Test public void test2() throws Exception{ Document doc = new SAXReader().read(new File("F:\c.xml")); Iterator<Element> it = doc.getRootElement().elementIterator("Student"); while(it.hasNext()){ Element stuElem = it.next(); if(stuElem.attributeValue("id").equals("002")){ stuElem.detach(); break; } } FileOutputStream out = new FileOutputStream("F:\c.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out,format); writer.write(doc); writer.close(); } }