为了账号安全,请及时绑定邮箱和手机立即绑定

dom4j解析xml入门

标签:
JavaScript

首先要导入dom4j的jar包

代码:

import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;public class dom4jDemo {        public static void main(String[] args) {                String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.seecom.com.cn/webservice\">"+                                     "   <soapenv:Header/>"+                                     "   <soapenv:Body>"+                                     "      <web:getChargeRequest>"+                                     "         <web:charge>"+                                     "            <web:channel>205</web:channel>"+                                     "            <web:pwd>fea1920da4045adeafda10bcd47f3c9f</web:pwd>"+                                     "            <web:orderNo>446666</web:orderNo>"+                                     "            <web:phone>13280009999</web:phone>"+                                     "            <web:money>12</web:money>"+                                     "         </web:charge>"+                                     "      </web:getChargeRequest>"+                                     "   </soapenv:Body>"+                                     "</soapenv:Envelope>";                Map<String, String> map = readXmlToMap(xml);                for (String key : map.keySet()) {                        System.out.println(key + "=" + map.get(key));                }        }        /**         * @功能描述:    解析提交结果         *         * @param xml         * @return          *         * @作者:zhangpj        @创建时间:2018年4月14日         */        public static Map<String, String> readXmlToMap(String xml) {                Document doc = null;                Map<String, String> resultMap = new HashMap<String, String>();                try {                        doc = DocumentHelper.parseText(xml); // 将字符串转为XML                        Element rootElt = doc.getRootElement(); // 获取根节点                        Element bodyElement = rootElt.element("Body");                        Element getChargeResponseElement = bodyElement.element("getChargeRequest");                        Element chargeElement = getChargeResponseElement.element("charge");                        try{                                // 方式一,直接获取//              String channel=chargeElement.elementTextTrim("channel"); //              String pwd=chargeElement.elementTextTrim("pwd");//              String orderNo=chargeElement.elementTextTrim("orderNo");//              String phone=chargeElement.elementTextTrim("phone");//              String money=chargeElement.elementTextTrim("money");//                    //              resultMap.put("channel", channel);//              resultMap.put("pwd", pwd);//              resultMap.put("orderNo", orderNo);//              resultMap.put("phone", phone);//              resultMap.put("money", money);                                // 方式二,迭代器获取                                  Iterator<Element> it = chargeElement.elementIterator();                                  // 遍历                                  while (it.hasNext()) {                                          // 获取某个子节点对象                                          Element e = it.next();                                          // 对子节点进行遍历                                          resultMap.put(e.getName(), e.getStringValue().trim());                                }                         }catch(Exception e){                                e.printStackTrace();                                //发生异常以后设置为处理中                                resultMap.put("status", "underway");                        }                } catch (Exception e) {                        e.printStackTrace();                }                return resultMap;        }}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消