我能够生成 SOAP 消息,但我不知道
只为soapMessage标签添加前缀(不应该有命名空间)
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage message = factory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPBody body = soapEnvelope.getBody(); soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix()); soapEnvelope.setPrefix("soap"); body.setPrefix("soap"); header.removeNamespaceDeclaration(header.getPrefix()); header.setPrefix("soap"); soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER"); Name bodyName; bodyName = soapFactory.createName("SearchHotels"); SOAPBodyElement getList = body.addBodyElement(bodyName); getList.setPrefix("v9"); Name childName = soapFactory.createName("SoapMessage", "v9", "URL TO SERVER"); SOAPElement HotelListRequest = getList.addChildElement(childName); HotelListRequest.addChildElement("Hotel", "v9").addTextNode("Hilton");
我的 SOAP 消息
...
<v9:SoapMessage xmlns:els="URL TO SERVER">
...
我的期望
...
<v9:SoapMessage>
...
更新 :
如果我使用以下内容,则会遇到以下错误
SOAPElement HotelListRequest = getList.addChildElement("v9:SoapMessage");
org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a
way which is incorrect with regard to namespaces.