java使用dom4j操作xml範例程式碼_java

來源:互聯網
上載者:User

dom4j是一個非常優秀的Java XML API,具有效能優異、功能強大和極端易用使用的特點,同時它也是一個開放源工具。可以在這個地址http://dom4j.sourceforge.net進行下載。
這裡我們使用到的dom4j是dom4j-1.6.1這個版本,我們只需要使用到如下兩個jar包:

複製代碼 代碼如下:

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

1、dom4j讀取xml字串

複製代碼 代碼如下:

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>灰機</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建立xml檔案

複製代碼 代碼如下:

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. 構造空的Document
        Document doc = DocumentHelper.createDocument();
        doc.addComment("this is a comment");
        // 2. 構造根項目
        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、讀取或寫xml檔案

讀取xml檔案

複製代碼 代碼如下:

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

寫xml檔案

複製代碼 代碼如下:

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

/建立document內容

複製代碼 代碼如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.