前言:
上一篇部落格我開始了用Dom4j對XML進行了讀操作,這篇部落客要是進行對XML的寫操作
隻涉及基礎的内容,隻要有javase基礎和eclipse的使用 就可以完成,
往後的内容包括 “架構” 我都還沒學到,是以本文中的内容也都是局限于基礎部分
有興趣的可以參考
Dom4j工具--XML的DOM解析(上)--讀操作也可以參考
DOM4J官網還可以檢視
DOM4J API目錄:
1. 如何寫内容到XML
2. 增:文檔,标簽,屬性,文本内容
3. 改:屬性值,文本
4. 删:标簽,屬性
現在開始正文。
如何寫内容到XML:
這一步是所有操作的前提,也是入門的必要操作
簡單舉個栗子:
實作對一個xml文檔的粘貼複制功能
@Test
public void test2() throws Exception {
// 1.讀取xml文檔,傳回Document對象
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");
OutputFormat outputFormat1=OutputFormat.createCompactFormat();
OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
outputFormat2.setEncoding("UTF-8");
XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
xmlWriter.write(doc);
xmlWriter.close();
}
結果:
createCompactFormat():
<?xml version="1.0" encoding="UTF-8"?>
<constant><cons><name>hello1</name></cons><name>hello2</name><cons><name>hello3</name></cons></constant>
createPrettyPrint():
<?xml version="1.0" encoding="UTF-8"?>
<constant>
<cons>
<name>hello1</name>
</cons>
<name>hello2</name>
<cons>
<name>hello3</name>
</cons>
</constant>
注意:
- @Test可以忽略 因為涉及到注解和單元測試,想了解單元測試的可以參考一下這篇: 單元測試--JUnit4了解一下(eclipse環境)
- XMLWriter導入的時候注意頭檔案别錯了 是:
import org.dom4j.io.XMLWriter;
- 這裡使用
而不是Writer字元流 因為防止考慮編碼FileOutputStream
- 記得最後要關閉流。
- createCompactFormat():緊湊的結構 去除空格和換行,項目上線 因為xml更小
-
createPrettyPrint(): 漂亮的結構 有空格和換行,開發調試
outputFormat2.setEncoding("UTF-8");是指定儲存的編碼格式為UTF-8
修改:
outputFormat2.setEncoding("GBK");内容是中文的話 就會出現亂碼
outputFormat2.setEncoding會使得 儲存的編碼格式和文檔聲明一緻 即
<?xml version="1.0" encoding="GBK"?>
增:文檔,标簽,屬性,文本内容:
DocumentHelper.createDocument()
增加文檔
addElement("名稱")
增加标簽
addAttribute("名稱",“值”)
增加屬性
addText(“内容”)
增加文本内容
源代碼:
@Test
public void test2() throws Exception {
FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");
Document doc=DocumentHelper.createDocument();//建立文檔
Element element=doc.addElement("age1"); //建立标簽
element.addAttribute("id", "12"); //建立屬性
element.addText("Text"); //建立文本檔案
OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
outputFormat2.setEncoding("UTF-8");
XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
xmlWriter.write(doc);
xmlWriter.close();
}
Element element=doc.addElement("age1");不能重複插入,因為隻有一個根标簽
修改:屬性值,文本:
Attribute.setValue("值") 修改屬性值
Element.addAtribute("同名的屬性名","值") 修改同名的屬性值
Element.setText("内容") 修改文本内容
@Test
public void test2() throws Exception {
// 1.讀取xml文檔,傳回Document對象
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");
//擷取标簽對象
Element element=doc.getRootElement();
Element elementSon=element.element("cons");
//修改屬性值
elementSon.addAttribute("id", "12"); // 通過增加同名屬性的方法,修改屬性值
Attribute attribute=elementSon.attribute("id"); // 擷取屬性對象 修改屬性值值
attribute.setValue("13");;
elementSon.setText("修改1"); //修改文本内容
OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
outputFormat2.setEncoding("UTF-8");
XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
xmlWriter.write(doc);
xmlWriter.close();
}
注意:cons标簽下先文本後name标簽,如果修改cons文本内容後 ,文本會下移到name标簽下。似乎沒啥大影響
删:标簽,屬性:
Element.detach(); 删除标簽
Attribute.detach(); 删除屬性
@Test
public void test2() throws Exception {
// 1.讀取xml文檔,傳回Document對象
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");
//擷取标簽對象
Element element=doc.getRootElement();
Element elementSon=element.element("cons");
//擷取到cons下第一個标簽節點并且删除
elementSon.elements().get(0).detach();
//擷取到cons标簽的屬性為id的屬性對象,然後删除
Attribute idAttribute=elementSon.attribute("id");
idAttribute.detach();
OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
outputFormat2.setEncoding("UTF-8");
XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
xmlWriter.write(doc);
xmlWriter.close();
}