亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消