DOM4j的使用

package TEST;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4j implements XMLDocument{

public static void main(String[] args) {
    XMLDocument doc = new Dom4j();      //創(chuàng)建dom4j對象

// doc.creatXml("a.xml"); //調(diào)用creatXml()方法,將內(nèi)容寫到a.xml文件中

    doc.parseXml("a.xml");              //調(diào)用parseXml解析方法,解析a.xml文件
}


public void creatXml(String filename) {
    
    Document document = DocumentHelper.createDocument();    //創(chuàng)建xml文檔,并加上頭部
    Element users = document.addElement("users");   //添加根節(jié)點users
    Element user = users.addElement("user");        //在根節(jié)點下添加二級節(jié)點user
    Element name = user.addElement("name");         //在二級節(jié)點user下添加三級節(jié)點name
    name.setText("張三");                         //在三級節(jié)點name中加上內(nèi)容
    user.addElement("age").setText("12");           //添加三級節(jié)點age,并加上內(nèi)容
    user.addElement("sex").setText("男");            //添加三級節(jié)點sex,并機(jī)上內(nèi)容
    user.addAttribute("id","001");                  //給user添加id屬性
    
    System.out.println(document.asXML());           //打印xml文檔
    
    Writer writer;
    try {

// writer = new FileWriter(filename);
// XMLWriter xmlwriter = new XMLWriter(writer);

        //包裝流,創(chuàng)建字節(jié)輸出流(utf-8格式,,解決字符集不匹配問題),再將字節(jié)輸出流轉(zhuǎn)成字符輸出流
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(filename),Charset.forName("UTF-8"));
        XMLWriter xmlwriter = new XMLWriter(ow);    //創(chuàng)建XML文檔寫入流
        xmlwriter.write(document);                  //寫入對象
        xmlwriter.close();                          //關(guān)閉流
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}

@Override
public void parseXml(String filename) {
    SAXReader reader = new SAXReader();
    Document document;
    try {
        document = reader.read(new FileInputStream(filename));
        Element root = document.getRootElement();       //得到xml文檔的根節(jié)點
        List<Element> elements = root.elements();       //得到根節(jié)點下的節(jié)點集合
        System.out.println("根節(jié)點下的節(jié)點總個數(shù)為:" + elements.size());
        for (Element e : elements) {        //輸出根節(jié)點下的每個節(jié)點
            System.out.println(e.getName());
            e.elements().forEach((child)->{
                Element c = (Element) child;
                System.out.println("\t" + c.getName() + " : " + c.getText());   //輸出根節(jié)點下的每個根節(jié)點和里面的內(nèi)容
            });
        }
        
    } catch (FileNotFoundException | DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    
}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容