生成信封特殊字符的问题

生成信封特殊字符的问题

问题描述:

我必须从字符串中创建一个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

什么是错误?发布堆栈跟踪。 –

每个XML标准有3个字符,必须是XML编码。请看这里的答案: xml-or-php-or-someone-is-eating-my-characters

顺便说一句:如果任何机会可能有二进制数据它也必须编码。作为Base64,十六进制或其他任何其他。

UPD:你在说什么特殊字符? 你的例子没有任何一个,它工作正常。 请将你的例外的“部分”部分的消息发布出去。

SAAJ0511: Unable to create envelope from given source是一条热门消息,并说没有用。