Dom4j递归输出所有的接点和值
 
这个程序是一个要显示程序调用关系xml配置,帮同事写的,拿出来分享下:
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;

/**
* Dom4j递归输出所有的接点和值
*
* @author leizhimin 12-11-9 下午3:48
*/

public class Test {

         public static void main(String[] args) throws DocumentException {
                Document doc = DocumentHelper.parseText(xml);
                Element root = doc.getRootElement();
                doit(root);
        }

         private static void doit(Element e) {
                List els = e.elements();
                 for (Element el : els) {
                         //判断是否复合节点
                         if(!el.hasMixedContent()){
                                System.out.println(el.getPath()+ ":"+el.getText());
                        } else{
                                System.out.println(el.getPath());
                                doit(el);
                        }
                }
        }

         private static String xml = "\n" +
                         "\n" +
                         "\t\n" +
                         "\t\taaaaaaaaaaaaaaaaaaaaaa\n" +
                         "\t\tmodifyService\n" +
                         "\t\t\n" +
                         "\t\t\tcom.asiainfo.aisc.sc.service.impl.FraScServiceInfoSVImpl1\n" +
                         "\t\t\tgetServiceInfo\n" +
                         "\t\t\n" +
                         "\t\t\n" +
                         "\t\t\tcom.ai.appframe2.complex.self.service.base.impl.BaseSVImpl2\n" +
                         "\t\t\tgetAllTableSplitFunction\n" +
                         "\t\t\t\n" +
                         "\t\t\t\tcom.ai.appframe2.complex.self.service.base.impl.AbcdeImpl3\n" +
                         "\t\t\t\tgetAbcdef\n" +
                         "\t\t\t\t\n" +
                         "\t\t\t\t\tcom.ai.appframe2.complex.self.service.base.impl.BaseSVImpl4\n" +
                         "\t\t\t\t\tgetAllDyncTableSplit\n" +
                         "\t\t\t\t\n" +
                         "\t\t\t\n" +
                         "\t\t\n" +
                         "\t\t\n" +
                         "\t\t\tcom.ai.appframe2.complex.self.service.base.impl.BaseSVImpl5\n" +
                         "\t\t\tgetAllTableSplitMapping\n" +
                         "\t\t\t\n" +
                         "\t\t\t\tcom.ai.appframe2.complex.self.service.base.impl.BaseSVImpl6\n" +
                         "\t\t\t\tgetAllIdGenerator\n" +
                         "\t\t\t\n" +
                         "\t\t\n" +
                         "\t\t\n" +
                         "\t\t\tcom.ai.appframe2.complex.self.service.base.impl.BaseSVImpl7\n" +
                         "\t\t\tgetAllIdGenerator\n" +
                         "\t\t\n" +
                         "\t\n" +
                         "\n";


}
 
输出结果:
/app/srv
/app/srv/cn:aaaaaaaaaaaaaaaaaaaaaa
/app/srv/mn:modifyService
/app/srv/srv
/app/srv/srv/cn:com.asiainfo.aisc.sc.service.impl.FraScServiceInfoSVImpl1
/app/srv/srv/mn:getServiceInfo
/app/srv/srv
/app/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl2
/app/srv/srv/mn:getAllTableSplitFunction
/app/srv/srv/srv
/app/srv/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.AbcdeImpl3
/app/srv/srv/srv/mn:getAbcdef
/app/srv/srv/srv/srv
/app/srv/srv/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl4
/app/srv/srv/srv/srv/mn:getAllDyncTableSplit
/app/srv/srv
/app/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl5
/app/srv/srv/mn:getAllTableSplitMapping
/app/srv/srv/srv
/app/srv/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl6
/app/srv/srv/srv/mn:getAllIdGenerator
/app/srv/srv
/app/srv/srv/cn:com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl7
/app/srv/srv/mn:getAllIdGenerator

Process finished with exit code 0