首页 > 其他 > 详细

dom4j的使用

时间:2015-06-20 02:09:35      阅读:258      评论:0      收藏:0      [点我收藏+]

??

-Node为所有的dom4jXML节点定义了多态行为;
?
-Branch为能够包含子节点的节点如XML元素(Element)和文档(Docuemnts)定义了一个公共的行为;
|-Element 定义XML 元素;
|-Document定义了XML文档;
?
-DocumentType 定义XML DOCTYPE声明;
-Entity定义 XML entity
-Attribute定义了XML的属性;
-ProcessingInstruction 定义 XML 处理指令;
?
-CharacterData是一个标识接口,标识基于字符的节点。如CDATAComment, Text
|- CDATA 定义了XML CDATA 区域;
|-Text 定义XML 文本节点;
|- Comment 定义了XML注释的行为;
?
创建一个XML
<?xml version="1.0" encoding="UTF-8"?>

<!--dom4j 示例用法--><?my dom4j demo?>

<ACCOUNTS author="zhangwei" size="2">
  <TAXACCOUNT>
    <NAME>*三</NAME>
    <USERID>2088123456789</USERID>
    <LOGINID>zha***@163.com</LOGINID>
    <IS_CERTIFY/>
    <MEMO><![CDATA[<TEST DOM4J>]]></MEMO>
  </TAXACCOUNT>
  <TAXACCOUNT>
    <NAME>*四</NAME>
    <USERID>2088123456790</USERID>
    <LOGINID>lis***@163.com</LOGINID>
    <MEMO><![CDATA[<TEST DOM4J>]]></MEMO>
    <IS_CERTIFY/>
  </TAXACCOUNT>
</ACCOUNTS>
?
 /**
     *
     * @return
     */
    private Document buildDocument() {
        /**
         * 写数据
         */

        Document doc = DocumentFactory.getInstance().createDocument();
        // 设置XML的字符编码
        doc.setXMLEncoding("UTF-8");
        // 增加注释
        doc.addComment("dom4j 示例用法");

        doc.addProcessingInstruction("my dom4j", "demo");
        // 增加节点元素
        Element root = doc.addElement("ACCOUNTS");
        //设置属性
        root.addAttribute("author", "zhangwei");
        root.addAttribute("size", "2");
        // 增加子节点,并设置节点值
        Element taxAccount = root.addElement("TAXACCOUNT");
        taxAccount.addElement("NAME").setText("*三");
        taxAccount.addElement("USERID").setText("2088123456789");
        taxAccount.addElement("LOGINID").setText("zha***@163.com");
        taxAccount.addElement("IS_CERTIFY");

        // 设置一个<![CDATA[]]>数据
        taxAccount.addElement("MEMO").addCDATA("<TEST DOM4J>");

        Element second = root.addElement("TAXACCOUNT");
        second.addElement("NAME").setText("*四");
        second.addElement("USERID").setText("2088123456790");
        second.addElement("LOGINID").setText("lis***@163.com");

        // 设置一个<![CDATA[]]>数据
        second.addElement("MEMO").addCDATA("<TEST DOM4J>");
        second.addElement("IS_CERTIFY");
        return doc;
    }
?将documnet写入到文件中
    private void writeXml2File(String fileName, Document document) {
        XMLWriter writer = null;
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"),
                format);

            writer.write(document);
        } catch (Exception e) {

        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception e) {

            }
        }
    }
??使用访问者模式读取XML
    @Test
    public void visitorXml() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read("D://dom4j.test.xml");
        document.accept(new VisitorSupport() {

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.Attribute)
             */
            @Override
            public void visit(Attribute node) {
                System.out.println("attribute:" + node.getName() + ":" + node.getData());
            }

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.CDATA)
             */
            @Override
            public void visit(CDATA node) {
                System.out.println("cdata " + node.getStringValue());
            }

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.Comment)
             */
            @Override
            public void visit(Comment node) {
                System.out.println("comment:" + node.getStringValue());
            }

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.Document)
             */
            @Override
            public void visit(Document document) {
                super.visit(document);
            }

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.Element)
             */
            @Override
            public void visit(Element node) {
                System.out.println("node " + node.getName() + ":" + node.getStringValue());
            }

            /**
             * @see org.dom4j.VisitorSupport#visit(org.dom4j.Text)
             */
            @Override
            public void visit(Text node) {
                System.out.println("text  :" + node.getStringValue());
            }

        });
    }
??标准JAVA迭代器模式读取XML
  /**
     * 标准的JAVA迭代模式
     *
     * @throws DocumentException
     */
    @SuppressWarnings("unchecked")
    @Test
    public void readXml() throws DocumentException {
        /**
         * 读数据
         */
        SAXReader reader = new SAXReader();
        Document document = reader.read("D://dom4j.test.xml");
        // 获取根节点
        Element root = document.getRootElement();
        // 遍历根节点下的所有节点
        for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
            Element e = it.next();
            readElement(e);
        }
    }

    private void readElement(Element element) {
        @SuppressWarnings("unchecked")
        Iterator<Element> ieson = element.elementIterator();
        if (ieson.hasNext()) {
            while (ieson.hasNext()) {
                Element elementSon = ieson.next();
                readElement(elementSon);
            }
        } else {
            System.out.println("---read---" + element.getName() + ":" + element.getText());
        }
    }
?
?

dom4j的使用

原文:http://zhangwei-david.iteye.com/blog/2220780

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!