dom4j(一)

其实我们用webService相互传递的都是xml而已,所以如果我们想用转成对象只有一个办法就是循环便利出所有节点

   下面在网上的一个例子

      

 import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.DocumentException;

import org.dom4j.Element;

 

import java.util.*;

 

/**

* Created by IntelliJ IDEA.

* User: leizhimin

* Date: 2008-4-14 14:02:12

* Note: Java递归遍历XML所有元素

*/

public class XmlTest {

 

//    private static Map xmlmap = new HashMap();

    //存储xml元素信息的容器

    private static List elemList = new ArrayList();

 

    //要测试的xml对象

    private static String srcXml = "/n" +

            "/n" +

            "    /n" +

            "        某人/n" +

            "                    /n" +

            "            /n" +

            "                10002/n" +

            "                西安市太白路/n" +

            "            /n" +

            "            /n" +

            "                10002/n" +

            "                ID节点啊/n" +

            "            /n" +

            "            /n" +

            "                10002/n" +

            "                ID节点啊/n" +

            "            /n" +

            "/t/t/t/n" +

            "/t/t/t/t/n" +

            "                西安市太白路2/n" +

            "            /n" +

            "/t/t/n" +

            "    /n" +

            "    /n" +

            "        ASDF/n" +

            "    /n" +

            "";

 

    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 elemList) {

        StringBuffer sb = new StringBuffer();

        for (Iterator it = elemList.iterator(); it.hasNext();) {

            Leaf leaf = it.next();

            sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue()).append("/n");

        }

        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内容------------

 

 

     

        某人

                    

            

                10002

                西安市太白路

            

            

                10002

                ID节点啊

            

            

                10002

                ID节点啊

            

      

    

                西安市太白路2

            

  

    

    

        ASDF

    

 

-----------解析结果------------

/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值