2017-09-27 130 views
0

我必須從字符串中創建一個SOAPMessage。生成信封特殊字符的問題

我的問題是,該字符串包含特殊字符,並在嘗試生成信封時給我一個錯誤。

我必須用特殊字符創建信封。

我只使用java 1.7的類,我需要使用代碼的類,我不能使用另一種類型的信封。

這是我的代碼

public class Main { 
    public static void main(String[] args) { 
    try { 
     //We have a envelope with special characters 
     String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><car:Input xmlns:car=\"http://soap.sctinst.redsys.es/consumed/CARGAENTIDADES\"><list><obj><name>José Televisión</name></obj></list></car:Input></soapenv:Body></soapenv:Envelope>"; 
     MimeHeaders headers = new MimeHeaders(); 
     headers.setHeader("Content-Type", "text/xml"); 
     headers.setHeader("Encoding", "UTF-8"); 

     ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes("UTF-8")); 
     int n = bis.available(); 
     byte[] bytes = new byte[n]; 
     bis.read(bytes, 0, n); 

     //I specify the encoding 
     String s = new String(bytes, StandardCharsets.UTF_8); 

     InputStream is = new ByteArrayInputStream(s.getBytes()); 

     //Create a new SOAPMessage 
     SOAPMessage message = MessageFactory.newInstance().createMessage(headers, is); 

     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     message.writeTo(out); 
     out.toString(); 
     //print the envelope and the especial characters are corrects 
     System.out.println(out.toString()); 
     SOAPPart soapPart = message.getSOAPPart(); 

     //FAIL THIS LINE 
     //com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope 
     //GRAVE: SAAJ0511: Unable to create envelope from given source 
     SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 


     SOAPHeader soapHeader = soapEnvelope.getHeader(); 
     SOAPBody soapBody = soapEnvelope.getBody(); 
     Source source = soapPart.getContent(); 
     //.... continue the code  
    } catch (SOAPException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

感謝

+0

什麼是錯誤?發佈堆棧跟蹤。 –

回答

0

每個XML標準有3個字符,必須是XML編碼。請看這裏的答案: xml-or-php-or-someone-is-eating-my-characters

順便說一句:如果任何機會可能有二進制數據它也必須編碼。作爲Base64,十六進制或其他任何其他。

UPD:你在說什麼特殊字符? 你的例子沒有任何一個,它工作正常。 請將你的例外的「部分」部分的消息發佈出去。

SAAJ0511: Unable to create envelope from given source是一條熱門消息,並說沒有用。