本文從以下幾個基礎的方面介紹dom4j操作XML的使用小結:
- [一] 讀取XML文件 的示例
- [二] 讀取XML字符串 的示例
- [三] 解析XML的document 的示例
- [四] XML編碼格式轉換 的示例
- [五] 輸出格式的自定義
的示例
- [六] XML輸出文件的示例
[一]、讀取XML文件:
xml示例的文件d:/test/michael/dom4j_info.xml,內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
讀取文件的Java代碼如下:
- String fileName = "d:/test/michael/dom4j_info.xml";
- try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File(fileName));
- System.out.println("document轉化爲String輸出如下:");
- System.out.println(document.asXML());
- } catch (Exception e) {
- e.printStackTrace();
- }
String fileName = "d:/test/michael/dom4j_info.xml";
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
System.out.println("document轉化爲String輸出如下:");
System.out.println(document.asXML());
} catch (Exception e) {
e.printStackTrace();
}
- document轉化爲String輸出如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
document轉化爲String輸出如下: <?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
ps:一般默認以XML文件中encoding定義的編碼格式讀取文件 。
[二]、讀取XML字符串:
Java讀取String的demo代碼:
- System.out.println("解析XML字符串DEMO");
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("document信息輸出,默認爲UTF-8的編碼:");
- System.out.println(document.asXML());
- } catch (Exception e) {
- e.printStackTrace();
- }
System.out.println("解析XML字符串DEMO");
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("document信息輸出,默認爲UTF-8的編碼:");
System.out.println(document.asXML());
} catch (Exception e) {
e.printStackTrace();
}
document信息輸出,默認爲UTF-8的編碼:
<?xml version="1.0" encoding="UTF-8"?>
<root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
ps:1.默認輸出編碼爲UTF-8
2.輸出字符串格式定義參見本文 第[五]部分
[三]、解析XML的Document
- /**
- * 解析文件
- * @blog http://sjsky.iteye.com
- * @param fileName
- */
- public static void readXMLFile(String fileName) {
- try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File(fileName));
- readXML(document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void readXML(Document document) {
- try {
- System.out.println("開始解析 XML Document:");
- // 獲取跟節點
- Element rootEle = document.getRootElement();
- System.out.println("獲取跟節點Root:");
- System.out.println(rootEle.asXML());
- // 根據名稱獲取Element
- Element info = rootEle.element("info");
- System.out.println("獲取Root下名稱爲info的子Element:");
- System.out.println(info.asXML());
- // 根據名稱獲取List<Element>
- List<Element> infolist = rootEle.elements("info");
- System.out.println("和上面獲取的Element應該一致:");
- System.out.println(infolist.get(0).asXML());
- // 獲取屬性值
- System.out.println("獲取指定屬性type的值 = " + info.attributeValue("type"));
- // 獲取所有的屬性值
- for (int i = 0; i < info.attributeCount(); i++) {
- System.out.println("獲取所有的屬性和值: " + info.attribute(i).getName()
- + " = " + info.attribute(i).getValue());
- }
- // XPath的功能展示
- Node node1 = document.selectSingleNode("/root/info/name");
- System.out.println("根據XPath=/root/info/name獲取document下的ELement : "
- + node1.asXML());
- Node node2 = rootEle.selectSingleNode("info/name");
- System.out.println("根據XPath=info/name 獲取root下的ELement : "
- + node2.asXML());
- Node node3 = rootEle.selectSingleNode("//info/name");
- System.out.println("根據XPath=//info/name 獲取root下的ELement : "
- + node3.asXML());
- System.out.println("上述三種方式獲取的結果應該是一致");
- List<?> nodeList = rootEle
- .selectNodes("//info/categories/category");
- System.out
- .println("根據XPath=//info/categories/category獲取root下的List:");
- for (int i = 0; i < nodeList.size(); i++) {
- System.out.println(((Element) nodeList.get(i)).asXML());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
/**
* 解析文件
* @blog http://sjsky.iteye.com
* @param fileName
*/
public static void readXMLFile(String fileName) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
readXML(document);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readXML(Document document) {
try {
System.out.println("開始解析 XML Document:");
// 獲取跟節點
Element rootEle = document.getRootElement();
System.out.println("獲取跟節點Root:");
System.out.println(rootEle.asXML());
// 根據名稱獲取Element
Element info = rootEle.element("info");
System.out.println("獲取Root下名稱爲info的子Element:");
System.out.println(info.asXML());
// 根據名稱獲取List<Element>
List<Element> infolist = rootEle.elements("info");
System.out.println("和上面獲取的Element應該一致:");
System.out.println(infolist.get(0).asXML());
// 獲取屬性值
System.out.println("獲取指定屬性type的值 = " + info.attributeValue("type"));
// 獲取所有的屬性值
for (int i = 0; i < info.attributeCount(); i++) {
System.out.println("獲取所有的屬性和值: " + info.attribute(i).getName()
+ " = " + info.attribute(i).getValue());
}
// XPath的功能展示
Node node1 = document.selectSingleNode("/root/info/name");
System.out.println("根據XPath=/root/info/name獲取document下的ELement : "
+ node1.asXML());
Node node2 = rootEle.selectSingleNode("info/name");
System.out.println("根據XPath=info/name 獲取root下的ELement : "
+ node2.asXML());
Node node3 = rootEle.selectSingleNode("//info/name");
System.out.println("根據XPath=//info/name 獲取root下的ELement : "
+ node3.asXML());
System.out.println("上述三種方式獲取的結果應該是一致");
List<?> nodeList = rootEle
.selectNodes("//info/categories/category");
System.out
.println("根據XPath=//info/categories/category獲取root下的List:");
for (int i = 0; i < nodeList.size(); i++) {
System.out.println(((Element) nodeList.get(i)).asXML());
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 開始解析 XML Document:
- 獲取跟節點Root:
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- 獲取Root下名稱爲info的子Element:
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- 和上面獲取的Element應該一致:
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- 獲取指定屬性type的值 = blog
- 獲取所有的屬性和值: index = 1
- 獲取所有的屬性和值: type = blog
- 根據XPath=/root/info/name獲取document下的ELement : <name id="sjsky">Michael</name>
- 根據XPath=info/name 獲取root下的ELement : <name id="sjsky">Michael</name>
- 根據XPath=//info/name 獲取root下的ELement : <name id="sjsky">Michael</name>
- 上述三種方式獲取的結果應該是一致
- 根據XPath=//info/categories/category獲取root下的List:
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
開始解析 XML Document: 獲取跟節點Root: <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> 獲取Root下名稱爲info的子Element: <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> 和上面獲取的Element應該一致: <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> 獲取指定屬性type的值 = blog 獲取所有的屬性和值: index = 1 獲取所有的屬性和值: type = blog 根據XPath=/root/info/name獲取document下的ELement : <name id="sjsky">Michael</name> 根據XPath=info/name 獲取root下的ELement : <name id="sjsky">Michael</name> 根據XPath=//info/name 獲取root下的ELement : <name id="sjsky">Michael</name> 上述三種方式獲取的結果應該是一致 根據XPath=//info/categories/category獲取root下的List: <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/>
ps:1. XPath的使用依賴lib包:jaxen.jar
2. XPath的簡單介紹說明(以後有機會做個詳細介紹)
- nodename 選取此節點的所有子節點(相對路徑)
- / 從根節點選取(絕對路徑)
- // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置
- . 選取當前節點
- .. 選取當前節點的父節點
- @ 選取屬性
[四]、XML編碼格式轉換:
- /**
- * @blog http://sjsky.iteye.com
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML編碼轉化DEMO");
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("默認爲UTF-8的編碼,輸出內容如下:");
- System.out.println(document.asXML());
- System.out.println("編碼轉化爲gb2312後,輸出內容如下:");
- String encodeStr = encodeXml(xmlStr, "gb2312");
- System.out.println(encodeStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
/**
* @blog http://sjsky.iteye.com
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML編碼轉化DEMO");
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("默認爲UTF-8的編碼,輸出內容如下:");
System.out.println(document.asXML());
System.out.println("編碼轉化爲gb2312後,輸出內容如下:");
String encodeStr = encodeXml(xmlStr, "gb2312");
System.out.println(encodeStr);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
- XML編碼轉化DEMO
- 默認爲UTF-8的編碼,輸出內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
- 編碼轉化爲gb2312後,輸出內容如下:
- <?xml version="1.0" encoding="gb2312"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- DEMO End---------------------
XML編碼轉化DEMO 默認爲UTF-8的編碼,輸出內容如下: <?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root> 編碼轉化爲gb2312後,輸出內容如下: <?xml version="1.0" encoding="gb2312"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> DEMO End---------------------
[五]、XML輸出格式的定義:
- OutputFormat compactFormat = OutputFormat.createCompactFormat(); 緊湊的格式
- OutputFormat prettyFormat = OutputFormat.createPrettyPrint(); 優雅具有層次的格式
- /**
- * @blog http://sjsky.iteye.com
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML輸出格式定義Demo:");
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("轉換爲優雅層次的格式輸出:");
- OutputFormat prettyFormat = OutputFormat.createPrettyPrint();
- String xmlPretty = xmlOutputFormat(document, prettyFormat);
- System.out.println(xmlPretty);
- System.out.println("轉換爲緊湊格式輸出:");
- OutputFormat compactFormat = OutputFormat.createCompactFormat();
- String xmlCompact = xmlOutputFormat(document, compactFormat);
- System.out.println(xmlCompact);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
- /**
- * XML 輸出格式轉換
- * @param document
- * @param opFormat
- * @return String
- */
- public static String xmlOutputFormat(Document document,
- OutputFormat opFormat) {
- // 具有層次的格式輸出
- // OutputFormat format = OutputFormat.createPrettyPrint();
- // 緊湊的格式輸出
- // OutputFormat format = OutputFormat.createCompactFormat();
- StringWriter sw = new StringWriter();
- try {
- XMLWriter writer = new XMLWriter(opFormat);
- writer.setWriter(sw);
- writer.write(document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sw.toString();
- }
/**
* @blog http://sjsky.iteye.com
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML輸出格式定義Demo:");
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("轉換爲優雅層次的格式輸出:");
OutputFormat prettyFormat = OutputFormat.createPrettyPrint();
String xmlPretty = xmlOutputFormat(document, prettyFormat);
System.out.println(xmlPretty);
System.out.println("轉換爲緊湊格式輸出:");
OutputFormat compactFormat = OutputFormat.createCompactFormat();
String xmlCompact = xmlOutputFormat(document, compactFormat);
System.out.println(xmlCompact);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
/**
* XML 輸出格式轉換
* @param document
* @param opFormat
* @return String
*/
public static String xmlOutputFormat(Document document,
OutputFormat opFormat) {
// 具有層次的格式輸出
// OutputFormat format = OutputFormat.createPrettyPrint();
// 緊湊的格式輸出
// OutputFormat format = OutputFormat.createCompactFormat();
StringWriter sw = new StringWriter();
try {
XMLWriter writer = new XMLWriter(opFormat);
writer.setWriter(sw);
writer.write(document);
} catch (Exception e) {
e.printStackTrace();
}
return sw.toString();
}
- XML輸出格式定義Demo:
- 轉換爲優雅層次的格式輸出:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- 轉換爲緊湊格式輸出:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
- DEMO End---------------------
XML輸出格式定義Demo: 轉換爲優雅層次的格式輸出: <?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> 轉換爲緊湊格式輸出: <?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root> DEMO End---------------------
[六]、XML輸出文件:
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML輸出文件:");
- String outFileName = "d:/test/michael/";
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- xmlWriteXMLDoc(outFileName+"dom4j_info_out1.xml", document);
- fileWriteXMLDoc(outFileName+"dom4j_info_out2.xml", document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
- /**
- *
- * @param fileName
- * @param document
- */
- public static void xmlWriteXMLDoc(String fileName, Document document) {
- XMLWriter writer = null;
- try {
- //writer = new XMLWriter(new FileWriter(fileName));
- //writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createCompactFormat());
- writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createPrettyPrint());
- writer.write(document);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != writer) {
- writer.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- *
- * @param fileName
- * @param document
- */
- public static void fileWriteXMLDoc(String fileName, Document document) {
- FileWriter fw = null;
- try {
- fw = new FileWriter(fileName);
- document.write(fw);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != fw) {
- fw.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
/**
* @blog http://sjsky.iteye.com
* @author Michael
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML輸出文件:");
String outFileName = "d:/test/michael/";
try {
Document document = DocumentHelper.parseText(xmlStr);
xmlWriteXMLDoc(outFileName+"dom4j_info_out1.xml", document);
fileWriteXMLDoc(outFileName+"dom4j_info_out2.xml", document);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
/**
*
* @param fileName
* @param document
*/
public static void xmlWriteXMLDoc(String fileName, Document document) {
XMLWriter writer = null;
try {
//writer = new XMLWriter(new FileWriter(fileName));
//writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createCompactFormat());
writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createPrettyPrint());
writer.write(document);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != writer) {
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @param fileName
* @param document
*/
public static void fileWriteXMLDoc(String fileName, Document document) {
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
document.write(fw);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fw) {
fw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成的dom4j_info_out1.xml文件內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
生成的dom4j_info_out2.xml文件內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
<?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
到此有關dom4j的簡單應用基本介紹完了,希望給新接觸的tx有所幫助。
本文從以下幾個基礎的方面介紹dom4j操作XML的使用小結:
- [一] 讀取XML文件 的示例
- [二] 讀取XML字符串 的示例
- [三] 解析XML的document 的示例
- [四] XML編碼格式轉換 的示例
- [五] 輸出格式的自定義
的示例
- [六] XML輸出文件的示例
[一]、讀取XML文件:
xml示例的文件d:/test/michael/dom4j_info.xml,內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
讀取文件的Java代碼如下:
- String fileName = "d:/test/michael/dom4j_info.xml";
- try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File(fileName));
- System.out.println("document轉化爲String輸出如下:");
- System.out.println(document.asXML());
- } catch (Exception e) {
- e.printStackTrace();
- }
String fileName = "d:/test/michael/dom4j_info.xml";
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
System.out.println("document轉化爲String輸出如下:");
System.out.println(document.asXML());
} catch (Exception e) {
e.printStackTrace();
}
- document轉化爲String輸出如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
document轉化爲String輸出如下: <?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
ps:一般默認以XML文件中encoding定義的編碼格式讀取文件 。
[二]、讀取XML字符串:
Java讀取String的demo代碼:
- System.out.println("解析XML字符串DEMO");
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("document信息輸出,默認爲UTF-8的編碼:");
- System.out.println(document.asXML());
- } catch (Exception e) {
- e.printStackTrace();
- }
System.out.println("解析XML字符串DEMO");
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("document信息輸出,默認爲UTF-8的編碼:");
System.out.println(document.asXML());
} catch (Exception e) {
e.printStackTrace();
}
document信息輸出,默認爲UTF-8的編碼:
<?xml version="1.0" encoding="UTF-8"?>
<root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
ps:1.默認輸出編碼爲UTF-8
2.輸出字符串格式定義參見本文 第[五]部分
[三]、解析XML的Document
- /**
- * 解析文件
- * @blog http://sjsky.iteye.com
- * @param fileName
- */
- public static void readXMLFile(String fileName) {
- try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File(fileName));
- readXML(document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void readXML(Document document) {
- try {
- System.out.println("開始解析 XML Document:");
- // 獲取跟節點
- Element rootEle = document.getRootElement();
- System.out.println("獲取跟節點Root:");
- System.out.println(rootEle.asXML());
- // 根據名稱獲取Element
- Element info = rootEle.element("info");
- System.out.println("獲取Root下名稱爲info的子Element:");
- System.out.println(info.asXML());
- // 根據名稱獲取List<Element>
- List<Element> infolist = rootEle.elements("info");
- System.out.println("和上面獲取的Element應該一致:");
- System.out.println(infolist.get(0).asXML());
- // 獲取屬性值
- System.out.println("獲取指定屬性type的值 = " + info.attributeValue("type"));
- // 獲取所有的屬性值
- for (int i = 0; i < info.attributeCount(); i++) {
- System.out.println("獲取所有的屬性和值: " + info.attribute(i).getName()
- + " = " + info.attribute(i).getValue());
- }
- // XPath的功能展示
- Node node1 = document.selectSingleNode("/root/info/name");
- System.out.println("根據XPath=/root/info/name獲取document下的ELement : "
- + node1.asXML());
- Node node2 = rootEle.selectSingleNode("info/name");
- System.out.println("根據XPath=info/name 獲取root下的ELement : "
- + node2.asXML());
- Node node3 = rootEle.selectSingleNode("//info/name");
- System.out.println("根據XPath=//info/name 獲取root下的ELement : "
- + node3.asXML());
- System.out.println("上述三種方式獲取的結果應該是一致");
- List<?> nodeList = rootEle
- .selectNodes("//info/categories/category");
- System.out
- .println("根據XPath=//info/categories/category獲取root下的List:");
- for (int i = 0; i < nodeList.size(); i++) {
- System.out.println(((Element) nodeList.get(i)).asXML());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
/**
* 解析文件
* @blog http://sjsky.iteye.com
* @param fileName
*/
public static void readXMLFile(String fileName) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
readXML(document);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readXML(Document document) {
try {
System.out.println("開始解析 XML Document:");
// 獲取跟節點
Element rootEle = document.getRootElement();
System.out.println("獲取跟節點Root:");
System.out.println(rootEle.asXML());
// 根據名稱獲取Element
Element info = rootEle.element("info");
System.out.println("獲取Root下名稱爲info的子Element:");
System.out.println(info.asXML());
// 根據名稱獲取List<Element>
List<Element> infolist = rootEle.elements("info");
System.out.println("和上面獲取的Element應該一致:");
System.out.println(infolist.get(0).asXML());
// 獲取屬性值
System.out.println("獲取指定屬性type的值 = " + info.attributeValue("type"));
// 獲取所有的屬性值
for (int i = 0; i < info.attributeCount(); i++) {
System.out.println("獲取所有的屬性和值: " + info.attribute(i).getName()
+ " = " + info.attribute(i).getValue());
}
// XPath的功能展示
Node node1 = document.selectSingleNode("/root/info/name");
System.out.println("根據XPath=/root/info/name獲取document下的ELement : "
+ node1.asXML());
Node node2 = rootEle.selectSingleNode("info/name");
System.out.println("根據XPath=info/name 獲取root下的ELement : "
+ node2.asXML());
Node node3 = rootEle.selectSingleNode("//info/name");
System.out.println("根據XPath=//info/name 獲取root下的ELement : "
+ node3.asXML());
System.out.println("上述三種方式獲取的結果應該是一致");
List<?> nodeList = rootEle
.selectNodes("//info/categories/category");
System.out
.println("根據XPath=//info/categories/category獲取root下的List:");
for (int i = 0; i < nodeList.size(); i++) {
System.out.println(((Element) nodeList.get(i)).asXML());
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 開始解析 XML Document:
- 獲取跟節點Root:
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- 獲取Root下名稱爲info的子Element:
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- 和上面獲取的Element應該一致:
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- 獲取指定屬性type的值 = blog
- 獲取所有的屬性和值: index = 1
- 獲取所有的屬性和值: type = blog
- 根據XPath=/root/info/name獲取document下的ELement : <name id="sjsky">Michael</name>
- 根據XPath=info/name 獲取root下的ELement : <name id="sjsky">Michael</name>
- 根據XPath=//info/name 獲取root下的ELement : <name id="sjsky">Michael</name>
- 上述三種方式獲取的結果應該是一致
- 根據XPath=//info/categories/category獲取root下的List:
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
開始解析 XML Document: 獲取跟節點Root: <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> 獲取Root下名稱爲info的子Element: <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> 和上面獲取的Element應該一致: <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> 獲取指定屬性type的值 = blog 獲取所有的屬性和值: index = 1 獲取所有的屬性和值: type = blog 根據XPath=/root/info/name獲取document下的ELement : <name id="sjsky">Michael</name> 根據XPath=info/name 獲取root下的ELement : <name id="sjsky">Michael</name> 根據XPath=//info/name 獲取root下的ELement : <name id="sjsky">Michael</name> 上述三種方式獲取的結果應該是一致 根據XPath=//info/categories/category獲取root下的List: <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/>
ps:1. XPath的使用依賴lib包:jaxen.jar
2. XPath的簡單介紹說明(以後有機會做個詳細介紹)
- nodename 選取此節點的所有子節點(相對路徑)
- / 從根節點選取(絕對路徑)
- // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置
- . 選取當前節點
- .. 選取當前節點的父節點
- @ 選取屬性
[四]、XML編碼格式轉換:
- /**
- * @blog http://sjsky.iteye.com
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML編碼轉化DEMO");
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("默認爲UTF-8的編碼,輸出內容如下:");
- System.out.println(document.asXML());
- System.out.println("編碼轉化爲gb2312後,輸出內容如下:");
- String encodeStr = encodeXml(xmlStr, "gb2312");
- System.out.println(encodeStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
/**
* @blog http://sjsky.iteye.com
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML編碼轉化DEMO");
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("默認爲UTF-8的編碼,輸出內容如下:");
System.out.println(document.asXML());
System.out.println("編碼轉化爲gb2312後,輸出內容如下:");
String encodeStr = encodeXml(xmlStr, "gb2312");
System.out.println(encodeStr);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
- XML編碼轉化DEMO
- 默認爲UTF-8的編碼,輸出內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
- 編碼轉化爲gb2312後,輸出內容如下:
- <?xml version="1.0" encoding="gb2312"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- DEMO End---------------------
XML編碼轉化DEMO 默認爲UTF-8的編碼,輸出內容如下: <?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root> 編碼轉化爲gb2312後,輸出內容如下: <?xml version="1.0" encoding="gb2312"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> DEMO End---------------------
[五]、XML輸出格式的定義:
- OutputFormat compactFormat = OutputFormat.createCompactFormat(); 緊湊的格式
- OutputFormat prettyFormat = OutputFormat.createPrettyPrint(); 優雅具有層次的格式
- /**
- * @blog http://sjsky.iteye.com
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML輸出格式定義Demo:");
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- System.out.println("轉換爲優雅層次的格式輸出:");
- OutputFormat prettyFormat = OutputFormat.createPrettyPrint();
- String xmlPretty = xmlOutputFormat(document, prettyFormat);
- System.out.println(xmlPretty);
- System.out.println("轉換爲緊湊格式輸出:");
- OutputFormat compactFormat = OutputFormat.createCompactFormat();
- String xmlCompact = xmlOutputFormat(document, compactFormat);
- System.out.println(xmlCompact);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
- /**
- * XML 輸出格式轉換
- * @param document
- * @param opFormat
- * @return String
- */
- public static String xmlOutputFormat(Document document,
- OutputFormat opFormat) {
- // 具有層次的格式輸出
- // OutputFormat format = OutputFormat.createPrettyPrint();
- // 緊湊的格式輸出
- // OutputFormat format = OutputFormat.createCompactFormat();
- StringWriter sw = new StringWriter();
- try {
- XMLWriter writer = new XMLWriter(opFormat);
- writer.setWriter(sw);
- writer.write(document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sw.toString();
- }
/**
* @blog http://sjsky.iteye.com
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML輸出格式定義Demo:");
try {
Document document = DocumentHelper.parseText(xmlStr);
System.out.println("轉換爲優雅層次的格式輸出:");
OutputFormat prettyFormat = OutputFormat.createPrettyPrint();
String xmlPretty = xmlOutputFormat(document, prettyFormat);
System.out.println(xmlPretty);
System.out.println("轉換爲緊湊格式輸出:");
OutputFormat compactFormat = OutputFormat.createCompactFormat();
String xmlCompact = xmlOutputFormat(document, compactFormat);
System.out.println(xmlCompact);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
/**
* XML 輸出格式轉換
* @param document
* @param opFormat
* @return String
*/
public static String xmlOutputFormat(Document document,
OutputFormat opFormat) {
// 具有層次的格式輸出
// OutputFormat format = OutputFormat.createPrettyPrint();
// 緊湊的格式輸出
// OutputFormat format = OutputFormat.createCompactFormat();
StringWriter sw = new StringWriter();
try {
XMLWriter writer = new XMLWriter(opFormat);
writer.setWriter(sw);
writer.write(document);
} catch (Exception e) {
e.printStackTrace();
}
return sw.toString();
}
- XML輸出格式定義Demo:
- 轉換爲優雅層次的格式輸出:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
- 轉換爲緊湊格式輸出:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
- DEMO End---------------------
XML輸出格式定義Demo: 轉換爲優雅層次的格式輸出: <?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root> 轉換爲緊湊格式輸出: <?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root> DEMO End---------------------
[六]、XML輸出文件:
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- * @param args
- */
- public static void main(String[] args) {
- String xmlStr = "<root><info index=\"1\" type=\"blog\">"
- + "<URL>http://sjsky.iteye.com</URL>"
- + "<name id=\"sjsky\">Michael</name>"
- + "<categories><category valule=\"java\"/>"
- + "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
- + "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
- + "</categories></info></root>";
- System.out.println("XML輸出文件:");
- String outFileName = "d:/test/michael/";
- try {
- Document document = DocumentHelper.parseText(xmlStr);
- xmlWriteXMLDoc(outFileName+"dom4j_info_out1.xml", document);
- fileWriteXMLDoc(outFileName+"dom4j_info_out2.xml", document);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DEMO End---------------------");
- }
- /**
- *
- * @param fileName
- * @param document
- */
- public static void xmlWriteXMLDoc(String fileName, Document document) {
- XMLWriter writer = null;
- try {
- //writer = new XMLWriter(new FileWriter(fileName));
- //writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createCompactFormat());
- writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createPrettyPrint());
- writer.write(document);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != writer) {
- writer.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- *
- * @param fileName
- * @param document
- */
- public static void fileWriteXMLDoc(String fileName, Document document) {
- FileWriter fw = null;
- try {
- fw = new FileWriter(fileName);
- document.write(fw);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != fw) {
- fw.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
/**
* @blog http://sjsky.iteye.com
* @author Michael
* @param args
*/
public static void main(String[] args) {
String xmlStr = "<root><info index=\"1\" type=\"blog\">"
+ "<URL>http://sjsky.iteye.com</URL>"
+ "<name id=\"sjsky\">Michael</name>"
+ "<categories><category valule=\"java\"/>"
+ "<category valule=\"spring\"/><category valule=\"hibernate\"/>"
+ "<category valule=\"NoSQL\"/><category valule=\"MYSQL\"/>"
+ "</categories></info></root>";
System.out.println("XML輸出文件:");
String outFileName = "d:/test/michael/";
try {
Document document = DocumentHelper.parseText(xmlStr);
xmlWriteXMLDoc(outFileName+"dom4j_info_out1.xml", document);
fileWriteXMLDoc(outFileName+"dom4j_info_out2.xml", document);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DEMO End---------------------");
}
/**
*
* @param fileName
* @param document
*/
public static void xmlWriteXMLDoc(String fileName, Document document) {
XMLWriter writer = null;
try {
//writer = new XMLWriter(new FileWriter(fileName));
//writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createCompactFormat());
writer = new XMLWriter(new FileWriter(fileName),OutputFormat.createPrettyPrint());
writer.write(document);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != writer) {
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @param fileName
* @param document
*/
public static void fileWriteXMLDoc(String fileName, Document document) {
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
document.write(fw);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fw) {
fw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成的dom4j_info_out1.xml文件內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <info index="1" type="blog">
- <URL>http://sjsky.iteye.com</URL>
- <name id="sjsky">Michael</name>
- <categories>
- <category valule="java"/>
- <category valule="spring"/>
- <category valule="hibernate"/>
- <category valule="NoSQL"/>
- <category valule="MYSQL"/>
- </categories>
- </info>
- </root>
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://sjsky.iteye.com</URL> <name id="sjsky">Michael</name> <categories> <category valule="java"/> <category valule="spring"/> <category valule="hibernate"/> <category valule="NoSQL"/> <category valule="MYSQL"/> </categories> </info> </root>
生成的dom4j_info_out2.xml文件內容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
<?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://sjsky.iteye.com</URL><name id="sjsky">Michael</name><categories><category valule="java"/><category valule="spring"/><category valule="hibernate"/><category valule="NoSQL"/><category valule="MYSQL"/></categories></info></root>
到此有關dom4j的簡單應用基本介紹完了,希望給新接觸的tx有所幫助。