带有javax.xml.soap的SOAP消息-名称空间错误?
以下是我应从Java Web应用程序调用的.NET Web服务的通用SOAP请求示例:
<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>
我可以使用以下代码段从Java控制台应用程序生成类似的内容:
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:
<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 尝试调用此服务,并像这样从WSDL生成了soap消息(它 前面的信封和前缀中的名称空间声明
):
<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中使用。但是最后,当我尝试使用以下代码序列重新创建这种形式的肥皂消息时:
// factories and stuff, like in the example aboveSOAPPart 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:试图以对名称空间不正确的方式创建或更改对象。
无论我尝试了什么,我都无法为 id 元素设置“ tem”前缀…这是怎么回事?
谢谢。
回答:
您正在将名称空间uri绑定到前缀,然后尝试创建具有相同前缀但名称空间uri为空的元素:
envelope.addNamespaceDeclaration("tem", "http://tempuri.org/");...
QName bodyName = new QName(null, "setAMRequestData", "tem");
元素由名称空间uri和本地名称的组合来标识。要解决此问题,必须在创建的每个元素上指定名称空间:
QName bodyName = new QName("http://tempuri.org/", "setAMRequestData", "tem");...
QName n = new QName("http://tempuri.org/", "id", "tem");
以上是 带有javax.xml.soap的SOAP消息-名称空间错误? 的全部内容, 来源链接: utcz.com/qa/400963.html