Dom4j递归输出所有的接点和值

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<Element> 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                         "<app>\n" +
                         "\t<srv>\n" +
                         "\t\t<cn>aaaaaaaaaaaaaaaaaaaaaa</cn>\n" +
                         "\t\t<mn>modifyService</mn>\n" +
                         "\t\t<srv>\n" +
                         "\t\t\t<cn>com.asiainfo.aisc.sc.service.impl.FraScServiceInfoSVImpl1</cn>\n" +
                         "\t\t\t<mn>getServiceInfo</mn>\n" +
                         "\t\t</srv>\n" +
                         "\t\t<srv>\n" +
                         "\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl2</cn>\n" +
                         "\t\t\t<mn>getAllTableSplitFunction</mn>\n" +
                         "\t\t\t<srv>\n" +
                         "\t\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.AbcdeImpl3</cn>\n" +
                         "\t\t\t\t<mn>getAbcdef</mn>\n" +
                         "\t\t\t\t<srv>\n" +
                         "\t\t\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl4</cn>\n" +
                         "\t\t\t\t\t<mn>getAllDyncTableSplit</mn>\n" +
                         "\t\t\t\t</srv>\n" +
                         "\t\t\t</srv>\n" +
                         "\t\t</srv>\n" +
                         "\t\t<srv>\n" +
                         "\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl5</cn>\n" +
                         "\t\t\t<mn>getAllTableSplitMapping</mn>\n" +
                         "\t\t\t<srv>\n" +
                         "\t\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl6</cn>\n" +
                         "\t\t\t\t<mn>getAllIdGenerator</mn>\n" +
                         "\t\t\t</srv>\n" +
                         "\t\t</srv>\n" +
                         "\t\t<srv>\n" +
                         "\t\t\t<cn>com.ai.appframe2.complex.self.service.base.impl.BaseSVImpl7</cn>\n" +
                         "\t\t\t<mn>getAllIdGenerator</mn>\n" +
                         "\t\t</srv>\n" +
                         "\t</srv>\n" +
                         "</app>\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

你可能感兴趣的:(dom4j)