Dom4j解析Xml的方法

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;

import org. dom4j.Attribute;
import org. dom4j.Document;
import org. dom4j.DocumentException;
import org. dom4j.DocumentHelper;
import org. dom4j.Element;
import org. dom4j.Node;
import org. dom4j.VisitorSupport;
import org. dom4j.io.OutputFormat;
import org. dom4j.io.SAXReader;
import org. dom4j.io.XMLWriter;

public class MyTest {
    /**
     * 自动遍历节点,可以重载很多方法。
     */
    public class MyVisitor extends VisitorSupport {
        public void visit(Attribute attr) {
            System.out.println(attr.getName());            //打印属性名
        }

        public void visit(Element element) {
            System.out.println(element.getName());         //打印元素名
        }
        public void visit(Element element) {

         System.out.println(element.getName()+":"+element.getData());
         //打印元素名:元素值
         System.out.println(element.hasMixedContent());
         //打印该元素是否是复合类型(是否包含子结点)
            
           
        }
    }

    private static String xmlPath = null;

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

        MyTest myTest = new MyTest();

        myTest.createDocument();

        myTest.getIterator(myTest.getRootElement(myTest.read(xmlPath)));

        myTest.treeWalk(xmlPath);

        myTest.getRootElement(myTest.read(xmlPath)).accept(myTest.new MyVisitor());

        myTest.getString(myTest.read(xmlPath));

    }

    /**
     ###################################################################################
     # 构造函数,定义文件的路径(和class放在一起)
     ###################################################################################
     */
    public MyTest() throws Exception {
        xmlPath = getClassFilePath().substring(1) + this.getClass().getName().replace('.', '/') + ".xml";
    }

    /**
     ###################################################################################
     # 生成XML文档
     ###################################################################################
     */
    public void createDocument() {

        Document document = DocumentHelper.createDocument();
        Element catalogElement = document.addElement("catalog");
        catalogElement.addComment("An XML Catalog");
        catalogElement.addProcessingInstruction("target", "text");
        Element journalElement = catalogElement.addElement("journal");
        journalElement.addAttribute("title", "XML Zone");
        journalElement.addAttribute("publisher", "IBM developerWorks");

        Element articleElement = journalElement.addElement("article");
        articleElement.addAttribute("level", "Intermediate");
        articleElement.addAttribute("date", "December-2001");
        Element titleElement = articleElement.addElement("title");
        titleElement.setText("Java configuration with XML Schema");
        Element authorElement = articleElement.addElement("author");
        Element firstNameElement = authorElement.addElement("firstname");
        firstNameElement.setText("Marcello");
        Element lastNameElement = authorElement.addElement("lastname");
        lastNameElement.setText("Vitaletti");

        document.addDocType("struts-config", "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", " http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd");
        //设置了之后在读取的时候总是去查找验证,暂时没有找到原因,以后找到了在贴上。
        //找到了解决方法,加上.


        try {
            this.saveXml(document, this.xmlPath, "GBK");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     ###################################################################################
     # 遍历属性节点(查找XHTML文档中所有的超链接)
     # 
     # @param document
     # @throws DocumentException
     #
     ###################################################################################
     */
    public void findLinks(Document document) throws DocumentException {

        List list = document.selectNodes("//a/@href");

        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Attribute attribute = (Attribute) iter.next();
            String url = attribute.getValue();
            System.out.println(url);
        }

    }

    /**
     ###################################################################################
     # 遍历元素的节点
     #
     # @param root
     # @throws Exception
     #
     ###################################################################################
     */
    public void getIterator(Element root) throws Exception {

        for (Iterator i = root.elementIterator(); i.hasNext();) {

            Element element = (Element) i.next();

            System.out.println(element);
        }
    }

    /**
     ###################################################################################
     # 返回该文档的根元素
     #
     # @param doc
     # @return
     #
     ###################################################################################
     */
    public Element getRootElement(Document doc) {

        return doc.getRootElement();

    }

    /**
     ###################################################################################
     # Xml 转 String
     #
     # @param document
     # @return
     #
     ###################################################################################
     */
    public void getString(Document document) {

        System.out.println(document.asXML());

        /*
         如果是String转换成XML:
         
         String text = <person> <name>James</name> </person>;
         
         Document document = DocumentHelper.parseText(text);

         */
    }

    /**
     ###################################################################################
     # 用SAX读入一个文档,返回这个Document
     #
     # @param fileName
     # @return
     # @throws MalformedURLException
     # @throws DocumentException
     #
     ###################################################################################
     */
    public Document read(String fileName) throws MalformedURLException, DocumentException {

        SAXReader reader = new SAXReader();

        reader.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
             
                InputStream dtdin=null;
    dtdin = this.getClass().getResourceAsStream("struts-config_1_1.dtd");
                //dtdin=new FileInputStream(new File("c:\\struts-config_1_1.dtd"));
                return new InputSource(dtdin);
            }
        });

        Document document = reader.read(new File(fileName));
        return document;

    }

    /**
     ###################################################################################
     # 格式化XML文档,并按指定字符集输出
     #
     # @param document
     # @param fileName
     # @param encoding
     # @return
     # @throws UnsupportedEncodingException
     # @throws FileNotFoundException
     # @throws IOException
     #
     ###################################################################################
     */
    public int saveXml(Document document, String fileName, String encoding) throws UnsupportedEncodingException, FileNotFoundException, IOException {

        int returnValue = 0;
        XMLWriter output = null;

        /** 格式化输出 , 类型 IE 浏览一样 */
        OutputFormat format = OutputFormat.createPrettyPrint();
        /** 命令行格式化输出*/
        //OutputFormat format = OutputFormat.createCompactFormat();
        format.setIndent("\t"); // 使用TAB缩进

        /** 指定 XML 字符集编码 */
        format.setEncoding(encoding);

        output = new XMLWriter(new FileOutputStream(new File(fileName)), format);
        output.write(document);
        output.close();

        /** 执行成功 , 需返回 1 */
        returnValue = 1;

        return returnValue;
    }

    /**
     ###################################################################################
     # 遍历传入元素下面所有节点
     #
     # @param element
     #
     ###################################################################################
     */
    public void treeWalk(Element element) {
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Element) {
                treeWalk((Element) node);
            } else {
            }
            System.out.println("<==" + node.getNodeTypeName() + "  " + node.getPath());
        }
    }

    /**
     * 
     ###################################################################################
     # 遍历所有节点
     #
     # @param fileName
     # @throws Exception
     #
     ###################################################################################
     */
    public void treeWalk(String fileName) throws Exception {
        treeWalk(getRootElement(read(fileName)));
    }

    /**
     ###################################################################################
     # 得到当前class文件的路径,像hib存放的那样。
     #
     # @return
     #
     ###################################################################################
     */
    private String getClassFilePath() throws Exception {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        URL sourceUrl = loader.getResource("");
//"test" 是当前包的路径,如果是“”系统返回的是你classes环境的根目录路径
        return URLDecoder.decode(sourceUrl.getFile(), "GBK");
    }

}






需要说明的是,在用 dom4j生成美观模式的文档在读取的时候会将回车换行当作文本来处理。关于修改的方法我没有加入进去,主要是比较简单,看看就会了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值