2016-02-15 20 views
2

我想从这样的字符串创建SOAP 1.2消息:为什么无法从给定源创建信封?

String soapIn = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
      "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" + 
      " <soapenv:Header />\n" + 
      " <soapenv:Body>\n" + 
      "  <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" + 
      "   <ns:return>\n" + 
      "    <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + 
      "    <response rows=\"1044\" type=\"success\">\n" + 
      "    </response>\n" + 
      "   </ns:return>\n" + 
      "  </ns:getChannelLineupInfoResponse>\n" + 
      " </soapenv:Body>\n" + 
      "</soapenv:Envelope>"; 
    MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); 
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to createMessage..."); 
    SOAPMessage newMsg = messageFactory.createMessage(
          new MimeHeaders(), 
          new ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8")))); 
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to get new envelope..."); 
    SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope(); //fails here 
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to get old envelope..."); 

但要“无法创建从给定的来源信封”在这一行google搜索失败的

SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope(); //fails here 

小时找到解决方案。有任何想法吗?

回答

0

似乎是一些图书馆问题。检查包括的罐子。下面的似乎是为我工作: -

import java.io.ByteArrayInputStream; 
import java.nio.charset.Charset; 

import javax.xml.soap.MessageFactory; 
import javax.xml.soap.MimeHeaders; 
import javax.xml.soap.SOAPConstants; 
import javax.xml.soap.SOAPEnvelope; 
import javax.xml.soap.SOAPMessage; 

public class Test { 

    public static void main(String[] args) throws Exception { 
     String soapIn = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
       "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" + 
       " <soapenv:Header />\n" + 
       " <soapenv:Body>\n" + 
       "  <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" + 
       "   <ns:return>\n" + 
       "    <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + 
       "    <response rows=\"1044\" type=\"success\">\n" + 
       "    </response>\n" + 
       "   </ns:return>\n" + 
       "  </ns:getChannelLineupInfoResponse>\n" + 
       " </soapenv:Body>\n" + 
       "</soapenv:Envelope>"; 
     MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); 
     System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to createMessage..."); 
     SOAPMessage newMsg = messageFactory.createMessage(
           new MimeHeaders(), 
           new ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8")))); 
     System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get new envelope..."); 
     SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope(); //fails here 
     System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get old envelope..."); 
     newMsg.writeTo(System.out); 
     System.out.println("**"); 
     System.out.println("Envolope " + newEnv); 
    } 
} 

输出是:

InneoquestSoapHandler.createNewSoapResponse: about to createMessage... 
InneoquestSoapHandler.createNewSoapResponse: about to get new envelope... 
InneoquestSoapHandler.createNewSoapResponse: about to get old envelope... 
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ns:getChannelLineupInfoResponse xmlns:ns="http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd"> 
      <ns:return> 
       <![CDATA[]]></ns:return> 
     </ns:getChannelLineupInfoResponse> 
    </soapenv:Body> 
</soapenv:Envelope>** 
Envolope soapenv:Envelope 
+0

谢谢!我的工作方式有所不同,但如果你能将它运用到图书馆,我会尽力说服你。我会接受你的回答。 – user3217883

0

我得到了它被加载到一个DOMSource的并用它来创建SOAP消息的工作:

 private SOAPMessage createNewSoapResponse(SOAPMessage msg) { 
      String soapIn = "<?xml version='1.0' encoding='UTF-8' ?>\n" + 
    "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" + 
    " <soapenv:Header />\n" + 
    " <soapenv:Body>\n" + 
    "  <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" + 
    "   <ns:return>\n" + 
    "    <response rows=\"1044\" type=\"success\">\n" + 
    "    </response>\n" + 
    "   </ns:return>\n" + 
    "  </ns:getChannelLineupInfoResponse>\n" + 
    " </soapenv:Body>\n" + 
    "</soapenv:Envelope>"; 
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      factory.setNamespaceAware(true); 
      SOAPMessage newMsg = null; 
      try { 
       DocumentBuilder builder = factory.newDocumentBuilder(); 
       InputSource inputSource = new InputSource(new StringReader(soapIn)); 
       Document doc = builder.parse(inputSource); 
       DOMSource domSource = new DOMSource(doc); 
       MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); 
ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8")))); 
       newMsg = messageFactory.createMessage(); 
       SOAPPart soapPart = newMsg.getSOAPPart(); 
       soapPart.setContent(domSource); 
       SOAPEnvelope newEnv = soapPart.getEnvelope(); 
       SOAPEnvelope oldEnv = msg.getSOAPPart().getEnvelope(); 
       addChannels(oldEnv,newEnv); 
      } catch (ParserConfigurationException ex) { 
       logger.debug("InneoquestSoapHandler.createNewSoapResponse: ParserConfigurationException="+ex.getMessage());  
      } catch (SAXException ex) { 
       logger.debug("InneoquestSoapHandler.createNewSoapResponse: SAXException="+ex.getMessage());  
      } catch (IOException ex) { 
       logger.debug("InneoquestSoapHandler.createNewSoapResponse: IOExeption="+ex.getMessage());  
      } catch (SOAPException ex) { 
       logger.debug("InneoquestSoapHandler.createNewSoapResponse: SOAPException="+ex.getMessage());  
      }  
      return newMsg; 
     } 

虽然我不得不删除这条线,以便解析器可以工作:

 "    <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + 

虽然它可能会工作,如果我把它放在一个CDATA标签。

相关问题