将XML嵌入SOAP请求中的CDATA部分时解析错误

时间:2014-02-22 03:50:24

标签: java xml soap escaping character

我正在尝试发送一条包含另一个XML的CDATA部分的soap消息,但是我收到了解析错误。我需要将XML数据嵌入CDATA部分。

这是我下面的代码;

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.AxisFault;

import com.levo.rec.*;

public class REC
{
    public static void main(String args[])
    {
        String mesaj = 
        "<![CDATA[                                                                         " + 
        "   <REC_DATA xmlns=\"http://www.levo.com/REC\">                                   " + 
        "   <RECheader RECID=\"\" REClabel=\"\"/>                                          " + 
        "       <RECattributes>                                                            " + 
        "           <attribute name=\"USERNAME\" value=\"" + userName + "\"/>              " + 
        "           <attribute name=\"DATA\" value=\"" + userData + "\"/>                  " + 
        "       </RECattributes>                                                           " + 
        "   </REC_DATA>                                                                    " + 
        "]]>                                                                               " ;

        try
        {
            y = new RECPortStub();
            y.sendMsg(mesaj);

        } catch (AxisFault e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (RemoteException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
“RECPortStub”类的代码如下;


public class RECPortStub extends org.apache.axis.client.Stub implements
        com.levo.rec.RECPort
{
    private java.util.Vector cachedSerClasses = new java.util.Vector();
    private java.util.Vector cachedSerQNames = new java.util.Vector();
    private java.util.Vector cachedSerFactories = new java.util.Vector();
    private java.util.Vector cachedDeserFactories = new java.util.Vector();

    static org.apache.axis.description.OperationDesc[] _operations;

    static
    {
        _operations = new org.apache.axis.description.OperationDesc[1];
        _initOperationDesc1();
    }

    private static void _initOperationDesc1()
    {
        org.apache.axis.description.OperationDesc oper;
        org.apache.axis.description.ParameterDesc param;
        oper = new org.apache.axis.description.OperationDesc();
        oper.setName("sendMsg");
        param = new org.apache.axis.description.ParameterDesc(
                new javax.xml.namespace.QName("", "inputXMLMessage"),
                org.apache.axis.description.ParameterDesc.IN,
                new javax.xml.namespace.QName(
                        "http://www.w3.org/2001/XMLSchema", "string"),
                java.lang.String.class, false, false);
        oper.addParameter(param);
        oper.setReturnType(new javax.xml.namespace.QName(
                "http://www.w3.org/2001/XMLSchema", "string"));
        oper.setReturnClass(java.lang.String.class);
        oper.setReturnQName(new javax.xml.namespace.QName("",
                "outputXMLMessage"));
        oper.setStyle(org.apache.axis.constants.Style.RPC);
        oper.setUse(org.apache.axis.constants.Use.ENCODED);
        _operations[0] = oper;

    }

    public RECPortStub() throws org.apache.axis.AxisFault
    {
        this(null);
    }

    public RECPortStub(java.net.URL endpointURL,
            javax.xml.rpc.Service service) throws org.apache.axis.AxisFault
    {
        this(service);
        super.cachedEndpoint = endpointURL;
    }

    public RECPortStub(javax.xml.rpc.Service service)
            throws org.apache.axis.AxisFault
    {
        if (service == null)
        {
            super.service = new org.apache.axis.client.Service();
        } else
        {
            super.service = service;
        }
        ((org.apache.axis.client.Service) super.service)
                .setTypeMappingVersion("1.2");
    }

    protected org.apache.axis.client.Call createCall()
            throws java.rmi.RemoteException
    {
        try
        {
            org.apache.axis.client.Call _call = super._createCall();
            if (super.maintainSessionSet)
            {
                _call.setMaintainSession(super.maintainSession);
            }
            if (super.cachedUsername != null)
            {
                _call.setUsername(super.cachedUsername);
            }
            if (super.cachedPassword != null)
            {
                _call.setPassword(super.cachedPassword);
            }
            if (super.cachedEndpoint != null)
            {
                _call.setTargetEndpointAddress(super.cachedEndpoint);
            }
            if (super.cachedTimeout != null)
            {
                _call.setTimeout(super.cachedTimeout);
            }
            if (super.cachedPortName != null)
            {
                _call.setPortName(super.cachedPortName);
            }
            java.util.Enumeration keys = super.cachedProperties.keys();
            while (keys.hasMoreElements())
            {
                java.lang.String key = (java.lang.String) keys.nextElement();
                _call.setProperty(key, super.cachedProperties.get(key));
            }
            return _call;
        } catch (java.lang.Throwable _t)
        {
            throw new org.apache.axis.AxisFault(
                    "Failure trying to get the Call object", _t);
        }
    }

    public java.lang.String sendMsg(java.lang.String inputXMLMessage)
            throws java.rmi.RemoteException
    {
        if (super.cachedEndpoint == null)
        {
            throw new org.apache.axis.NoEndPointException();
        }
        org.apache.axis.client.Call _call = createCall();
        _call.setOperation(_operations[0]);
        _call.setUseSOAPAction(true);
        _call.setSOAPActionURI("");
        _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
        _call.setOperationName(new javax.xml.namespace.QName(
                "http://hedehodo.com/RECadapter",
                "sendMsg"));

        setRequestHeaders(_call);
        setAttachments(_call);
        try
        {
            java.lang.Object _resp = _call
                    .invoke(new java.lang.Object[] { inputXMLMessage });

            if (_resp instanceof java.rmi.RemoteException)
            {
                throw (java.rmi.RemoteException) _resp;
            } else
            {
                extractAttachments(_call);
                try
                {
                    return (java.lang.String) _resp;
                } catch (java.lang.Exception _exception)
                {
                    return (java.lang.String) org.apache.axis.utils.JavaUtils
                            .convert(_resp, java.lang.String.class);
                }
            }
        } catch (org.apache.axis.AxisFault axisFaultException)
        {
            throw axisFaultException;
        }
    }

}

1 个答案:

答案 0 :(得分:3)

注意:我的问题中的xml格式不正确,但我只是用它来测试,如果 Axis 更改&#39;&lt;&#39;&#39; &GT;&#39;字符(转义码)转义字符,如&#39;&lt;&#39;和&#39;&gt;&#39;在XML的 CDATA部分内。

有两种解决方案;

  1. (简易解决方案)使用 SAAJ 代替 Axis 进行SOAP处理。 SAAJ 不会转义CDATA部分内的字符。

  2. 重写Axis方法,以便在XML的CDATA部分内,字符按原样传递而不是转义字符。在CDATA部分之外,小于或大于的字符应该由于xml文档而被转义,但是在CDATA部分内,它们应该按原样传递(据我所知)。