zoukankan      html  css  js  c++  java
  • 递归遍历XML所有节点

    package xml;
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    
    import java.util.*;
    
    /**
     * @author zouhailin
     * 2013-7-5
     */
    public class XmlTest {
    
    //    private static Map<String, String> xmlmap = new HashMap<String, String>();
        //存储xml元素信息的容器
        private static List<String> elemList = new ArrayList<String>();
    
        //要测试的xml对象
        private static String srcXml = "<?xml version="1.0" encoding="GBK"?>
    " +
                "<doc>
    " +
                "    <person>
    " +
                "        <name>某人</name>
    " +
                "        <adds>            
    " +
                "            <add ID="10002">
    " +
                "                <BS>10002</BS>
    " +
                "                <note>西安市太白路</note>
    " +
                "            </add>
    " +
                "            <add ID="">
    " +
                "                <BS>10002</BS>
    " +
                "                <note>空ID节点啊</note>
    " +
                "            </add>
    " +
                "            <add>
    " +
                "                <BS>10002</BS>
    " +
                "                <note>空ID节点啊</note>
    " +
                "            </add>
    " +
                "			<add ID="10001">
    " +
                "				<BS xmlns="10001"/>
    " +
                "                <note>西安市太白路2</note>
    " +
                "            </add>
    " +
                "		</adds>
    " +
                "    </person>
    " +
                "    <other>
    " +
                "        <name ID="HEHE">ASDF</name>
    " +
                "    </other>
    " +
                "</doc>";
    
        public static void main(String args[]) throws DocumentException {
            XmlTest test = new XmlTest();
            Element root = test.getRootElement();
            test.getElementList(root);
            String x = test.getListString(elemList);
    
            System.out.println("-----------原xml内容------------");
            System.out.println(srcXml);
            System.out.println("-----------解析结果------------");
            System.out.println(x);
    
        }
    
        /**
         * 获取根元素
         *
         * @return
         * @throws DocumentException
         */
        public Element getRootElement() throws DocumentException {
            Document srcdoc = DocumentHelper.parseText(srcXml);
            Element elem = srcdoc.getRootElement();
            return elem;
        }
    
        /**
         * 递归遍历方法
         *
         * @param element
         */
        public void getElementList(Element element) {
            List elements = element.elements();
            if (elements.size() == 0) {
                //没有子元素
                String xpath = element.getPath();
                String value = element.getTextTrim();
                elemList.add(xpath+" "+value);
            } else {
                //有子元素
                for (Iterator it = elements.iterator(); it.hasNext();) {
                    Element elem = (Element) it.next();
                    //递归遍历
                    getElementList(elem);
                }
            }
        }
    
        public String getListString(List<String> elemList) {
            StringBuffer sb = new StringBuffer();
            for (Iterator<String> it = elemList.iterator(); it.hasNext();) {
                String str = it.next();
                sb.append(str+"
    ");
            }
            return sb.toString();
        }
    } 


  • 相关阅读:
    用Apache Kafka构建流数据平台
    kafka与传统的消息中间件对比
    Azkaban简介和使用
    kettle初探
    less命令
    spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
    Spring 注释标签@Resource @Autowired 和@Inject的区别
    【MyEclipse 2015】 逆向破解实录系列【2】(纯研究)
    HDOJ 5073 Galaxy 数学 贪心
    windows 8.0上eclipse 4.4.0 配置centos 6.5 上的hadoop2.2.0开发环境
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3174445.html
Copyright © 2011-2022 走看看