首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

dom4j运用小结(基础入门级)

2012-10-12 
dom4j使用小结(基础入门级)?? blog迁移至:http://www.micmiu.com?本文从以下几个基础的方面介绍dom4j操作X

dom4j使用小结(基础入门级)

?? blog迁移至:http://www.micmiu.com

?

本文从以下几个基础的方面介绍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>

?读取文件的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();        }

?

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();        }
<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();        }    }
??
开始解析 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---------------------");    }

?

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();    }

?

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();            }        }    }

?

生成的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>

?

生成的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>

到此有关dom4j的简单应用基本介绍完了,希望给新接触的tx有所帮助。

?

?

?

热点排行