javax.xml.soap中与SOAP消息 - 命名空间的错误? [英] SOAP message with javax.xml.soap - namespace error?
问题描述
以下是.NET Web服务的通用样本SOAP请求我应该从我的Java Web应用程序调用:
The following is a generic sample SOAP request for .NET web service I'm supposed to invoke from my java web app:
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<setAMRequestData xmlns="http://tempuri.org/">
<id>int</id>
</setAMRequestData>
</soap:Body>
</soap:Envelope>
我可以用这个code段生成从Java控制台应用程序类似:
I am able to generate something similar from java console application using this code segment:
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
...
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", XMLConstants.DEFAULT_NS_PREFIX);
SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
QName n = new QName("id");
SOAPElement quotation = bodyElement.addChildElement(n);
quotation.addTextNode("121152");
结果是下面的XML:
The result is the following XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<setAMRequestData xmlns="http://tempuri.org/">
<id>121152</id>
</setAMRequestData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这调用服务。接下来,我用的的soapUI 的尝试调用这个服务,它在信封中的命名空间声明生成的SOAP消息从WSDL这样的(它的不同从previous和prefixes):
This invokes the service. Next, I used soapUI to try to invoke this service, and it generated soap message from WSDL like this (it differs from the previous in the namespace declaration in envelope, and prefixes):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:setAMRequestData>
<tem:id>?</tem:id>
</tem:setAMRequestData>
</soapenv:Body>
</soapenv:Envelope>
这也从工作soapUI的。但最后,当我试图用这个code序列重建这种形式的SOAP消息:
This also works from soapUI. But finally, when I tried to recreate this form of soap message using this code sequence:
// factories and stuff, like in the example above
SOAPPart part = sm.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
QName bodyName = new QName(null, "setAMRequestData", "tem");
SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
QName n = new QName(null, "id", "tem");
SOAPElement quotation = bodyElement.addChildElement(n);
quotation.addTextNode("7028");
我得到了线以下异常的的SOAPElement报价= bodyElement.addChildElement(N); 的:
org.w3c.dom.DOMException中:NAMESPACE_ERR:一个试图创建或在某种程度上这是不正确关于命名空间改变对象
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.
不管我试了,我根本就没有设置的标识的元素......这到底是怎么回事统preFIX?
No matter what I tried, I simply couldn't set "tem" prefix for the id element... What is going on here?
感谢。
推荐答案
您是一个命名空间URI绑定到preFIX,后来尝试用相同的preFIX但有一个空的命名空间URI创建一个元素:
You are binding a namespace uri to a prefix and later try to create an element with the same prefix but with a null namespace uri:
envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");
...
QName bodyName = new QName(null, "setAMRequestData", "tem");
的元素是由空间URI和本地名称的组合标识。为了解决这个问题,你必须指定创建的每个元素的命名空间:
An element is identified by the combination of namespace uri and local name. To solve this problem you have to specify the namespace on each element you create:
QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", "tem");
...
QName n = new QName("http://tempuri.org/", "id", "tem");
这篇关于javax.xml.soap中与SOAP消息 - 命名空间的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!