dom4j操作xml的demo,dom4j操作xmldemo

來源:互聯網
上載者:User

dom4j操作xml的demo,dom4j操作xmldemo

package com.cn.shop.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

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

/**
*
* @author NH
*
*/
public class XmlUtils {

public static Document getDocument() {

// 1.讀取xml檔案擷取document對象
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read("D:\\itext\\27663.xml");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2.通過解析xml的文本
/*
* String xmlFilePath = "D:\\itext\\27663.xml"; try { document =
* DocumentHelper.parseText(xmlFilePath); } catch (DocumentException e)
* { // TODO Auto-generated catch block e.printStackTrace(); } // 3.通過
* Document document = DocumentHelper.createDocument(); Element root =
* document.addElement("csdn");
*/
return document;
}

public static void anaXml() throws Exception {

// 讀取xml的常值內容來建立document對象
SAXReader reader = new SAXReader();

try {
Document document = reader.read("D:\\itext\\27663.xml");

Element root = document.getRootElement();

System.out.println(root.getName());
getElement(root);
/* elementMethod(root); */

/*
* // 擷取一個節點 Element element = root.element("title");
*
*
* //擷取element的id屬性節點對象 Attribute attr = element.attribute("id");
* //刪除屬性 element.remove(attr);
*
* // 添加新屬性 element.addAttribute("author", "作者");
*
* // 添加新的節點 Element newElement = root.addElement("where"); //
* 設定新節點的值 newElement.setText("北京人民出版社,天津人民大學出版社");
*
* // 擷取element中的where元素節點對象 Element author =
* element.element("where"); // 刪除元素節點 boolean flag =
* element.remove(author); // 返回true代碼刪除成功,否則失敗
* System.out.println(flag); // 添加CDATA地區
* element.addCDATA("紅樓夢,是一部愛情小說."); // 寫入到一個新的檔案中 writer(document);
*/

} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/*
*
* 所有節點內容
*/
public static void getElement(Element root) {
// 擷取當前節點的所有屬性節點
List<Attribute> as = root.attributes();

for (Attribute a : as) {

System.out.println("當前屬性節點的名稱:" + a.getName());

/*
* System.out.println("當前屬性節點的內容:" + a.getText());
*
* System.out.println("當前屬性節點的值:" + a.getValue());
*/

}

if (!root.getTextTrim().equals("")) {

System.out.println("常值內容::::" + root.getText());
}

Iterator<Element> el = root.elementIterator();

while (el.hasNext()) {

// 擷取某個子節點對象
Element e = el.next();
// 對子節點進行遍曆
getElement(e);
}

}

/**
* 介紹Element中的element方法和elements方法的使用
*
* @param node
*/
public static void elementMethod(Element node) {
// 擷取node節點中,子節點的元素名稱為西遊記的元素節點。
Element e = node.element("info");
// 擷取西遊記元素節點中,子節點為chapter的元素節點(可以看到只能擷取第一個作者元素節點)
Element author = e.element("classification");

System.out.println(e.getName() + "----" + author.getText());

// 擷取西遊記這個元素節點 中,所有子節點名稱為classification元素的節點 。
List<Element> authors = e.elements("classification");
for (Element aut : authors) {
System.out.println(aut.getText());
}

// 擷取西遊記這個元素節點 所有元素的子節點。
List<Element> elements = e.elements();

for (Element el : elements) {
System.out.println(el.getText());
}

}

/**
* 把document對象寫入新的檔案
*
* @param document
* @throws Exception
*/
public static void writer(Document document) throws Exception {
// 緊湊的格式
// OutputFormat format = OutputFormat.createCompactFormat();
// 排版縮排的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 設定編碼
format.setEncoding("UTF-8");
// 建立XMLWriter對象,指定了寫出檔案及編碼格式
/*
* XMLWriter writer = new XMLWriter(new OutputStreamWriter(new
* FileOutputStream(new File("src//a.xml")), "UTF-8"), format);
*/

File file = new File("c://index//大主宰.xml");

FileOutputStream fos = new FileOutputStream(file);

OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

XMLWriter writer = new XMLWriter(osw);

// 寫入
writer.write(document);
// 立即寫入
writer.flush();
// 關閉操作
writer.close();
}

// 以下的代碼為字串與xml互轉執行個體
public void test() throws Exception {

// 建立saxreader對象
SAXReader reader = new SAXReader();
// 讀取一個檔案,把這個檔案轉換成Document對象
Document document = reader.read(new File("src//c.xml"));
// 擷取根項目
Element root = document.getRootElement();
// 把文檔轉換字串
String docXmlText = document.asXML();
System.out.println(docXmlText);
System.out.println("---------------------------");
// csdn元素標籤根轉換的內容
String rootXmlText = root.asXML();
System.out.println(rootXmlText);
System.out.println("---------------------------");
// 擷取java元素標籤 內的內容
Element e = root.element("java");
System.out.println(e.asXML());

}

/**
* 建立一個document對象 往document對象中添加節點元素 轉存為xml檔案
*
* @throws Exception
*/
public void test2() throws Exception {

Document document = DocumentHelper.createDocument();// 建立根節點
Element root = document.addElement("csdn");
Element java = root.addElement("java");
java.setText("java班");
Element ios = root.addElement("ios");
ios.setText("ios班");

writer(document);
}

/**
* 把一個文本字串轉換Document對象
*
* @throws Exception
*/
public void test1() throws Exception {
String text = "<csdn><java>Java班</java><net>Net班</net></csdn>";
Document document = DocumentHelper.parseText(text);
Element e = document.getRootElement();
System.out.println(e.getName());
writer(document);
}

/**
* 把document對象寫入新的檔案
*
* @param document
* @throws Exception
*/
public void writer1(Document document) throws Exception {
// 緊湊的格式
// OutputFormat format = OutputFormat.createCompactFormat();
// 排版縮排的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 設定編碼
format.setEncoding("UTF-8");
// 建立XMLWriter對象,指定了寫出檔案及編碼格式
// XMLWriter writer = new XMLWriter(new FileWriter(new
// File("src//a.xml")),format);
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src//c.xml")), "UTF-8"),
format);
// 寫入
writer.write(document);
// 立即寫入
writer.flush();
// 關閉操作
writer.close();
}

public static void main(String[] args) {

try {
anaXml();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.