WS - XML报文

摘要:当SoapUI访问一个webservice时,对于某些webserive服务,如果webserive的输入参数要求是xml格式,如果xml格式输入不正确,会报“Unmarshalling Error: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs. at [row,col {unknown-source}]: [9,14]”的错误信息。

问题描述

当SoapUI访问一个webservice时,对于某些webserive服务,如果webserive的输入参数要求是xml格式,如果xml格式输入不正确,会报“Unmarshalling Error: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs. at [row,col {unknown-source}]: [9,14]”的错误信息。

客户现场案例描述:

某客户ESB支持时,客户这边提供一个发送短信的webservice,其中一个输入参数要求是xml格式,如下图所示:

错误1:当将xml格式的输入参数(即<xmlbody>内容)设置为:

<SmsPush>
<phone>13600000000</phone>
<content>This is a test sms ! </content>
<sendTime/>
<transactionId/>
</SmsPush>

时,进行webservice调用,报错如下:

Unmarshalling Error: unexpected element (uri:"", local:"SmsPush"). Expected elements are (none)

如下图所示:

错误2:添加<xml>标头信息后,当将xml格式的输入参数(即<xmlbody>内容)设置为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SmsPush>
<phone>13600000000</phone>
<content>This is a test sms ! </content>
<sendTime/>
<transactionId/>
</SmsPush>

时,进行webservice调用,报错如下:

Unmarshalling Error: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.at [row,col {unknown-source}]: [9,15] 

如下图所示:

正确xml参数输入格式:

将xml格式的输入参数(即<xmlbody>内容)设置为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SmsPush> <phone>13679122863</phone> <content>this is test </content> <sendTime/> <transactionId/> </SmsPush>

如下图所示:

这样再进行webservice调用,就可以调用成功了。如下图所示:

解释:

对于问题1所出现的错误,是因为输入参数要求要xml格式,而所给出的参数没有添加<?xml version="1.0" encoding="UTF-8"?>,所以其形式并不满足xml标准,因此不能够成功调用成功。

对于问题2所出现的错误,虽然输入参数已经符合xml的标准格式,但是由于转义符号的原因,所以出现了这样的错误。

因此,在输入xml格式的参数时,首先需要添加<?xml version="1.0" encoding="UTF-8"?>(注意:这一行必须出现在第一行,并且需要顶格,前面没有任何空格或其他字符),使得输入参数符合xml标准;其次对于转义字符,需要进行改变。

常用转义字符:

总结:

主线:xml中不能含有xml,里面的xml必须转义。

扩展:dom4J,CXF等框架会自动识别和转义报文。

特殊:针对于服务器端,参数一般需要加上cdata使数据准确无误,或者base64加密,不会出现特殊字符。

附程序:

** 
      * 替换一个字符串中的某些指定字符 
      * @param strData String 原始字符串 
      * @param regex String 要替换的字符串 
      * @param replacement String 替代字符串 
      * @return String 替换后的字符串 
      */  
     public static String replaceString(String strData, String regex,  
             String replacement)  
     {  
         if (strData == null)  
         {  
             return null;  
         }  
         int index;  
         index = strData.indexOf(regex);  
         String strNew = "";  
         if (index >= 0)  
         {  
             while (index >= 0)  
             {  
                 strNew += strData.substring(0, index) + replacement;  
                 strData = strData.substring(index + regex.length());  
                 index = strData.indexOf(regex);  
             }  
             strNew += strData;  
             return strNew;  
         }  
         return strData;  
     }  
      
     /** 
      * 替换字符串中特殊字符 
      */  
    public static String encodeString(String strData)  
     {  
         if (strData == null)  
         {  
             return "";  
         }  
         strData = replaceString(strData, "&", "&amp;");  
         strData = replaceString(strData, "<", "&lt;");  
         strData = replaceString(strData, ">", "&gt;");  
         strData = replaceString(strData, "&apos;", "&apos;");  
         strData = replaceString(strData, "\"", "&quot;");  
         return strData;  
     }  
      
     /** 
      * 还原字符串中特殊字符 
      */  
    public static String decodeString(String strData)  
     {  
         strData = replaceString(strData, "&lt;", "<");  
         strData = replaceString(strData, "&gt;", ">");  
         strData = replaceString(strData, "&apos;", "&apos;");  
         strData = replaceString(strData, "&quot;", "\"");  
         strData = replaceString(strData, "&amp;", "&");  
         strData = replaceString(strData, "&#xD;", "");  
         return strData;  
     }  

 

posted on 2016-07-11 17:19  TrustNature  阅读(478)  评论(0)    收藏  举报