xml的四种解析方法4(DOM4J)

来源(jump the world)

From: http://hi.baidu.com/hkkkhk1984/blog/item/8e282b1259d324c6c3fd7815.html

2009-06-03 17:19DOM4J解析

dom4j与JDOM一样,也是一种用于解析XML文档的开放源代码的XML框架,dom4j也应用于java平台,dom4j API使用了java集合框架并完全支持DOM、SAX和JAXP。与JDOM不同的是,dom4j使用接口和抽象类,虽然dom4j的API相对复杂些,但它提供了比JDOM更好的灵活性。dom4j也使用SAX解析器来分析XML文档,创建dom4j树。此外dom4j也可以接收DOM格式的内容,并提供了从dom4j树到SAX事件流或W3C DOM树的输出机制。与JDOM不同,dom4j自带了一个SAX解析器Aelfred2,如果没有显示的设置SAX解析器,也没有通过系统属性org.xml.sax.driver设置解析器,dom3j将会使用JAXP来加载JAXP配置的解析器,如果创建解析器失败,那么最后才使用dom4j自带的Aelfred2解析器。
同样,首先要在工程中添加dom4j的jar包,这里使用dom4j-1.6.1.jar。(见附件)
Dom4j生成XML文档db.xml:Dom4jCreate.java Java代码
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Dom4jCreate {

    public static void main(String[] args) {

        Document doc = DocumentHelper.createDocument();

        doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl' href='db.xsl'");
        doc.addDocType("dbconfig", null,"db.dtd");

        //Element root=DocumentHelper.createElement("dbconfig");
        // doc.setRootElement(root);
        Element root = doc.addElement("dbconfig");

        Element eltDb= root.addElement("db");
        Element eltDriver = eltDb.addElement("driver");
        Element eltUrl = eltDb.addElement("url");
        Element eltUser = eltDb.addElement("user");
        Element eltPassword = eltDb.addElement("password");

        eltDriver.setText("com.mysql.jdbc.Driver");
        eltUrl.setText("jdbc:mysql://localhost/mySql");
        eltUser.setText("root");
        eltPassword.setText("xlc");
        eltDb.addAttribute("type","mysql");

        try {

            //设置输出格式
            OutputFormat outFmt = new OutputFormat("    ", true);
            outFmt.setEncoding("UTF-8");

            /**//*PrintWriter pw = new PrintWriter(System.out);
            doc.write(pw);
            pw.flush();
            pw.close();*/

            XMLWriter xmlWriter = new XMLWriter(System.out, outFmt);
            // XMLWriter xmlWriter=new XMLWriter(new FileWriter("db.xml"),outFmt);
            xmlWriter.write(doc);
            xmlWriter.flush();
            xmlWriter.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}



Dom4j修改XML文档db.xml:Dom4jModify.java

Java代码
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jModify {

    public Document modifyDocument(File inputXml) {

    try {

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputXml);
        document.addDocType("dbconfig",null,"db.dtd");
        List list = document.content();

        // Iterator iter = document.nodeIterator();
        Iterator iter = list.iterator();

        Element element = (Element) iter.next();
        element.element("db").attribute("type").setValue("mysql");
        element.element("db").element("url").setText("jdbc:mysql://localhost/mySql");
        element.element("db").element("driver").setText("com.mysql.jdbc.Driver");
        element.element("db").element("user").setText("root");
        element.element("db").element("password").setText("xlc");

        // 设置输出格式
        OutputFormat outFmt = new OutputFormat("     ", true);
        outFmt.setEncoding("UTF-8");

        XMLWriter xmlWriter=new XMLWriter(new FileWriter("domdb-modified.xml"),outFmt);
        xmlWriter.write(document);
        xmlWriter.flush();
        xmlWriter.close();
        return document;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {

        Dom4jModify dom4jParser = new Dom4jModify();
        Document document = dom4jParser.modifyDocument(new File("domdb.xml"));

        OutputFormat outFmt = new OutputFormat("    ", true);
        outFmt.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(System.out,outFmt);
        xmlWriter.write(document);
        xmlWriter.flush();
        xmlWriter.close();
    }
}

Link: http://www.asm32.net/article_details.aspx?id=4494


浏览次数 69 发布时间 2009/6/10 10:44:28 从属分类 Java 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1