欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用dom4j操作xml示例代碼

 更新時間:2013年11月29日 09:33:45   作者:  
dom4j是一個Java的XML API,類似于jdom,用來讀寫XML文件,下面我來個小例子學(xué)習(xí)他的使用方法

dom4j是一個非常優(yōu)秀的Java XML API,具有性能優(yōu)異、功能強(qiáng)大和極端易用使用的特點(diǎn),同時它也是一個開放源工具??梢栽谶@個地址http://dom4j.sourceforge.net進(jìn)行下載。
這里我們使用到的dom4j是dom4j-1.6.1這個版本,我們只需要使用到如下兩個jar包:

復(fù)制代碼 代碼如下:

dom4j-1.6.1.jar
commons-io-2.4.jar

1、dom4j讀取xml字符串

復(fù)制代碼 代碼如下:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class TestReadXMLString {
    public static void main(String[] args) throws DocumentException {
        String readline = "<?xml version=\"1.0\" encoding=\"utf-8\"?><students><student sid=\"001\"> <id>001</id><name>灰機(jī)</name> <age>18</age> </student></students>";
        Document document = DocumentHelper.parseText(readline);
        Element rootElm = document.getRootElement();
        System.out.println("rootElement:  " + rootElm.getName());
        Element student = rootElm.element("student");
        Element id = student.element("id");
        Element name = student.element("name");
        Element age = student.element("age");
        System.out.println(id.getText());
        System.out.println(name.getText());
        System.out.println(age.getText());
    }
}

2、dom4j創(chuàng)建xml文件

復(fù)制代碼 代碼如下:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
public class TestWriteXMLString {
    public static void main(String[] args) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // 1. 構(gòu)造空的Document
        Document doc = DocumentHelper.createDocument();
        doc.addComment("this is a comment");
        // 2. 構(gòu)造根元素
        Element rootElmt = doc.addElement("users");
        rootElmt.addNamespace("test", "www.test.com");

        Element userElmt = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1001");
        userElmt.addElement("name").setText("zhangsan");
        userElmt.addElement("age").setText("20");
        userElmt.addElement("gender").setText("mail");

        Element userElmt2 = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1002");
        userElmt2.addElement("name").setText("zhangsan");
        userElmt2.addElement("age").setText("20");
        userElmt2.addElement("gender").setText("mail");

        System.out.println(doc.asXML().replaceAll("\n", ""));
    }
}

3、讀取或?qū)憍ml文件

讀取xml文件

復(fù)制代碼 代碼如下:

SAXReader reader = new SAXReader();
String path = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = reader.read(new File(path));

寫xml文件

復(fù)制代碼 代碼如下:

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");// 設(shè)置XML文件的編碼格式
String filePath = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = DocumentHelper.createDocument();
doc.addComment("this is a comment");

/創(chuàng)建document內(nèi)容

復(fù)制代碼 代碼如下:

XMLWriter writer = new XMLWriter(new FileWriter(filePath), format);//寫入指定的文件
writer.write(document);
 writer.close();

相關(guān)文章

最新評論