本文从以下几个基础的方面介绍dom4j操作XML的使用小结:
- [一] 读取XML文件 的示例
- [二] 读取XML字符串 的示例
- [三] 解析XML的document 的示例
- [四] XML编码格式转换 的示例
- [五] 输出格式的自定义 的示例
- [六] XML输出文件的示例
[一]、读取XML文件:
xml示例的文件d:/test/michael/dom4j_info.xml,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://www.micmiu.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代码如下:
1 2 3 4 5 6 7 8 9 |
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(); } |
运行结果输出如下:
123456789101112131415 document转化为String输出如下:<?xml version="1.0" encoding="UTF-8"?><root><info index="1" type="blog"><URL>http://www.micmiu.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代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
System.out.println("解析XML字符串DEMO"); String xmlStr = "<root><info index=\"1\" type=\"blog\">" + "<URL>http://www.micmiu.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(); } |
运行结果输出如下:
解析XML字符串DEMO
document信息输出,默认为UTF-8的编码:
<?xml version=”1.0″ encoding=”UTF-8″?>
<root><info index=”1″ type=”blog”><URL>http://www.micmiu.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** * 解析文件 * @blog http://www.micmiu.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(); } } |
运行结果如下:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 开始解析 XML Document:获取跟节点Root:<root><info index="1" type="blog"><URL>http://www.micmiu.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://www.micmiu.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://www.micmiu.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编码格式转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * @blog http://www.micmiu.com * @param args */ public static void main(String[] args) { String xmlStr = "<root><info index=\"1\" type=\"blog\">" + "<URL>http://www.micmiu.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---------------------"); } |
运行结果如下:
12345678910111213141516171819202122 XML编码转化DEMO默认为UTF-8的编码,输出内容如下:<?xml version="1.0" encoding="UTF-8"?><root><info index="1" type="blog"><URL>http://www.micmiu.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://www.micmiu.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(); 优雅具有层次的格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/** * @blog http://www.micmiu.com * @param args */ public static void main(String[] args) { String xmlStr = "<root><info index=\"1\" type=\"blog\">" + "<URL>http://www.micmiu.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(); } |
运行结果输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
XML输出格式定义Demo: 转换为优雅层次的格式输出: <?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://www.micmiu.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://www.micmiu.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输出文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/** * @blog http://www.micmiu.com * @author Michael * @param args */ public static void main(String[] args) { String xmlStr = "<root><info index=\"1\" type=\"blog\">" + "<URL>http://www.micmiu.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文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <root> <info index="1" type="blog"> <URL>http://www.micmiu.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文件内容如下:
1 2 |
<?xml version="1.0" encoding="UTF-8"?> <root><info index="1" type="blog"><URL>http://www.micmiu.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有所帮助。
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/opensource/dom4j-start-sample/
0 条评论。