dom4j 对含有命名空间的xml进行解析

待解析xml报文

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns1:queryBalanceBonResponse xmlns:ns1="http://chinasoftinc.com/BonWebservice/">
         <result>0result>
         <errorCode>0errorCode>
         <errorMessage>查询余额成功errorMessage>
         <paymentFlag>0paymentFlag>
         <totalBalanceAvailable>20totalBalanceAvailable>
         <balanceDetail>
            <balanceInfo>
               <balanceTypeFlag>0balanceTypeFlag>
               <BalanceAvailable>20BalanceAvailable>
            balanceInfo>
            <balanceInfo>
               <balanceTypeFlag>1balanceTypeFlag>
               <BalanceAvailable>0BalanceAvailable>
            balanceInfo>
         balanceDetail>
      ns1:queryBalanceBonResponse>
   soapenv:Body>
soapenv:Envelope>

报文对应的bean
QueryModel .java

package cn.com.min.dom4j;

import java.util.ArrayList;
import java.util.List;

public class QueryModel {
    private String  result ="";
    private String  errorCode ="";
    private String  errorMessage ="";
    private String  paymentFlag ="";
    private String  totalBalanceAvailable ="";
    private List balanceInfos =new ArrayList();
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }
    public String getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    public String getPaymentFlag() {
        return paymentFlag;
    }
    public void setPaymentFlag(String paymentFlag) {
        this.paymentFlag = paymentFlag;
    }
    public String getTotalBalanceAvailable() {
        return totalBalanceAvailable;
    }
    public void setTotalBalanceAvailable(String totalBalanceAvailable) {
        this.totalBalanceAvailable = totalBalanceAvailable;
    }
    public List getBalanceInfos() {
        return balanceInfos;
    }
    public void setBalanceInfos(List balanceInfos) {
        this.balanceInfos = balanceInfos;
    }




}

BalanceInfoListModel.java

package cn.com.min.dom4j;

public class BalanceInfoListModel {
    private String balanceTypeFlag = "";
    private String BalanceAvailable = "";
    public String getBalanceTypeFlag() {
        return balanceTypeFlag;
    }
    public void setBalanceTypeFlag(String balanceTypeFlag) {
        this.balanceTypeFlag = balanceTypeFlag;
    }
    public String getBalanceAvailable() {
        return BalanceAvailable;
    }
    public void setBalanceAvailable(String balanceAvailable) {
        BalanceAvailable = balanceAvailable;
    }



}

解析类
XML2Bean.java

package cn.com.min.dom4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

public class XML2Bean {

    public static String getNotNullNodeValue(Node n) {
        return getNotNullNodeValue(n, null);
    }

    public static String getNotNullNodeValue(Node n, String valueType) {
        if (n == null)
            return "";
        else
            return n.getStringValue().trim();
    }

    public static QueryModel xmlToQueryModel(String xml) throws DocumentException{ //异常直接抛出去了
        Map map = new HashMap();
        Document doc = DocumentHelper.parseText(xml);
        QueryModel queryModel =new QueryModel();
        Element rootElrm= doc.getRootElement();
       // map.put("soapenv","http://schemas.xmlsoap.org/soap/envelope/");
       // map.put("ns1","http://chinasoftinc.com/BonWebservice/");
        Element el = rootElrm.element("Body");
        Element node = el.element("queryBalanceBonResponse");
        System.out.println(node.asXML());
       // XPath x = doc.createXPath("//soapenv:Body/ns1:queryBalanceBonResponse");
      //  x.setNamespaceURIs(map);
      //  List nodelist = x.selectNodes(doc);
      //  Node node = (Node) nodelist.get(0);
        //System.out.println(node.asXML());
        //queryModel.setErrorCode(XML2Bean.getNotNullNodeValue(node.selectSingleNode("result")));
        //System.out.println(node.selectSingleNode("result").getText());

        queryModel.setResult(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorCode")));
        queryModel.setErrorMessage(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorMessage")));
        queryModel.setPaymentFlag(XML2Bean.getNotNullNodeValue(node.selectSingleNode("paymentFlag")));
        queryModel.setTotalBalanceAvailable(XML2Bean.getNotNullNodeValue(node.selectSingleNode("totalBalanceAvailable")));

        List nodes = node.selectNodes("balanceDetail/balanceInfo");

        if (nodes != null && nodes.size()>0) {
            List balanceInfos =new ArrayList();
            for(Node nod :nodes){

                BalanceInfoListModel  balanceInfoListModel = new BalanceInfoListModel();
                balanceInfoListModel.setBalanceTypeFlag(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("balanceTypeFlag")));
                balanceInfoListModel.setBalanceAvailable(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+nod.selectSingleNode("balanceTypeFlag").getText());
                balanceInfos.add(balanceInfoListModel);
            }
            queryModel.setBalanceInfos(balanceInfos);               
        }   
        return queryModel;
    }

    public static void main(String[] args) {
        String xml1 ="00查询余额成功020020010";

        try {
            QueryModel queryModel = XML2Bean.xmlToQueryModel(xml1);
            System.out.println(queryModel.getErrorCode());
        } catch (DocumentException e) {
            System.out.println(e);
        }
    }

}

以上源码可直接使用

其中主要的解析方法 public static QueryModelxmlToQueryModel(String xml)

其中使用两种方式进行含命名空间的xml进行解析

public static QueryModel xmlToQueryModel(String xml) throws DocumentException{ //异常直接抛出去了
        Document doc = DocumentHelper.parseText(xml);
        QueryModel queryModel =new QueryModel();
        Element rootElrm= doc.getRootElement();

        Element el = rootElrm.element("Body");
        Element node = el.element("queryBalanceBonResponse");
        System.out.println(node.asXML());

queryModel.setErrorCode(XML2Bean.getNotNullNodeValue(node.selectSingleNode("result"))); 

queryModel.setResult(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorCode")));
        queryModel.setErrorMessage(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorMessage")));
        queryModel.setPaymentFlag(XML2Bean.getNotNullNodeValue(node.selectSingleNode("paymentFlag")));
        queryModel.setTotalBalanceAvailable(XML2Bean.getNotNullNodeValue(node.selectSingleNode("totalBalanceAvailable")));

        List nodes = node.selectNodes("balanceDetail/balanceInfo");

        if (nodes != null && nodes.size()>0) {
            List balanceInfos =new ArrayList();
            for(Node nod :nodes){

                BalanceInfoListModel  balanceInfoListModel = new BalanceInfoListModel();
                balanceInfoListModel.setBalanceTypeFlag(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("balanceTypeFlag")));
                balanceInfoListModel.setBalanceAvailable(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+nod.selectSingleNode("balanceTypeFlag").getText());
                balanceInfos.add(balanceInfoListModel);
            }
            queryModel.setBalanceInfos(balanceInfos);               
        }   
        return queryModel;
    }
public static QueryModel xmlToQueryModel(String xml) throws DocumentException{ //异常直接抛出去了
        Map map = new HashMap();
        Document doc = DocumentHelper.parseText(xml);
        QueryModel queryModel =new QueryModel();
        Element rootElrm= doc.getRootElement();
        map.put("soapenv","http://schemas.xmlsoap.org/soap/envelope/");
        map.put("ns1","http://chinasoftinc.com/BonWebservice/");
        XPath x = doc.createXPath("//soapenv:Body/ns1:queryBalanceBonResponse");
        x.setNamespaceURIs(map);
        List nodelist = x.selectNodes(doc);
        Node node = (Node) nodelist.get(0);
        queryModel.setErrorCode(XML2Bean.getNotNullNodeValue(node.selectSingleNode("result")));

        queryModel.setResult(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorCode")));
        queryModel.setErrorMessage(XML2Bean.getNotNullNodeValue(node.selectSingleNode("errorMessage")));
        queryModel.setPaymentFlag(XML2Bean.getNotNullNodeValue(node.selectSingleNode("paymentFlag")));
        queryModel.setTotalBalanceAvailable(XML2Bean.getNotNullNodeValue(node.selectSingleNode("totalBalanceAvailable")));

        List nodes = node.selectNodes("balanceDetail/balanceInfo");

        if (nodes != null && nodes.size()>0) {
            List balanceInfos =new ArrayList();
            for(Node nod :nodes){

                BalanceInfoListModel  balanceInfoListModel = new BalanceInfoListModel();
                balanceInfoListModel.setBalanceTypeFlag(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("balanceTypeFlag")));
                balanceInfoListModel.setBalanceAvailable(XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+XML2Bean.getNotNullNodeValue(nod.selectSingleNode("BalanceAvailable")));
                System.out.println("-->"+nod.selectSingleNode("balanceTypeFlag").getText());
                balanceInfos.add(balanceInfoListModel);
            }
            queryModel.setBalanceInfos(balanceInfos);               
        }   
        return queryModel;
    }

其中主要用的用到两个java包
dom4j-1.6.1.jar
jaxen-1.1.1.jar

你可能感兴趣的:(java,dom4j)