技术中心

这里象征着我们的态度和能力

>Dom4j递归遍历XML所有元素
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-28      浏览次数:6174
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
做一个递归遍历XML的例子,为更为复杂的解析工作做基础。
 
目标:遍历所有的元素节点,并且取出来其中的值,结果打印到控制台。
 
源代码如下:
本程序依赖DOM4j包。
 
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import java.util.*;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-14 14:02:12<br>
* <b>Note</b>: Java递归遍历XML所有元素
*/

public class XmlTest {

//    private static Map<String, String> xmlmap = new HashMap<String, String>();
    //存储xml元素信息的容器
    private static List<Leaf> elemList = new ArrayList<Leaf>();

    //要测试的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(new Leaf(xpath, value));
        } else {
            //有子元素
            for (Iterator it = elements.iterator(); it.hasNext();) {
                Element elem = (Element) it.next();
                //递归遍历
                getElementList(elem);
            }
        }
    }

    public String getListString(List<Leaf> elemList) {
        StringBuffer sb = new StringBuffer();
        for (Iterator<Leaf> it = elemList.iterator(); it.hasNext();) {
            Leaf leaf = it.next();
            sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue()).append(" ");
        }
        return sb.toString();
    }
}

/**
* xml节点数据结构
*/

class Leaf {
    private String xpath;         //
    private String value;

    public Leaf(String xpath, String value) {
        this.xpath = xpath;
        this.value = value;
    }

    public String getXpath() {
        return xpath;
    }

    public void setXpath(String xpath) {
        this.xpath = xpath;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
 
运行结果:
 
-----------原xml内容------------
<?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>
-----------解析结果------------
/doc/person/name = 某人
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 西安市太白路
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 空ID节点啊
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 空ID节点啊
/doc/person/adds/add/*[name()=BS] =  
/doc/person/adds/add/note = 西安市太白路2
/doc/other/name = ASDF


Process finished with exit code 0
 
可以发现,有很多xpath相同的值域。
4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730