gpt4 book ai didi

java - 在有效的 SOAP SAAJ 信封中使用默认命名空间

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:18:05 26 4
gpt4 key购买 nike

这两个 SOAP 消息有效吗?不同的部分是 namespace 属性 xmlns="http://www.sigvalue.com/acc"。第一个 soap 是一个示例,第二个由 java 代码生成以生成相同的 soap 消息。

<?xml version="1.0" encoding="utf-8"?>
<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>
<GetNGPList xmlns="http://www.sigvalue.com/acc">
<UserData>
<senderId>string</senderId>
<channelId>string</channelId>
<timeStamp>dateTime</timeStamp>
</UserData>
<zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>

.

<?xml version="1.0" encoding="utf-8"?>
<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>
<GetNGPList>
<UserData xmlns="http://www.sigvalue.com/acc">
<senderId>string</senderId>
<channelId>string</channelId>
<timeStamp>dateTime</timeStamp>
</UserData xmlns="http://www.sigvalue.com/acc">
<zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>

如果第二个 SOAP 无效,我怎么能使它和第一个一样? GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");这条线没有按预期工作。这是 JAVA 代码:

    MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();

SOAPPart soapPart = soapMessage.getSOAPPart();


// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi", gXSIServerURI);
envelope.addNamespaceDeclaration("xsd", gXSDServerURI);

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList = soapBody.addChildElement("GetNGPList");
GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");

SOAPElement UserData = GetNGPList.addChildElement("UserData");
SOAPElement senderId = UserData.addChildElement("senderId");
SOAPElement channelId = UserData.addChildElement("channelId");
SOAPElement timeStamp = UserData.addChildElement("timeStamp");
senderId.addTextNode("string");
channelId.addTextNode("string");
timeStamp.addTextNode("dateTime");

SOAPElement zipCode = GetNGPList.addChildElement("zipCode");
zipCode.addTextNode("string");


MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "sample");

soapMessage.saveChanges();

/* Print the request message */
//System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);

最佳答案

要将命名空间设置为默认命名空间,只需使用空字符串 ("") 作为前缀名称:

SOAPElement GetNGPList =
soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面的代码会将 xmlns="http://www.sigvalue.com/acc" 应用于 GetNGPList

你的代码,改编:

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList =
soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");

SOAPElement UserData = GetNGPList.addChildElement("UserData");
...

像往常一样,当您在 addChildElement() 中省略命名空间前缀声明时,子项将从其父项继承其命名空间。

上面的代码将生成您需要的内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<GetNGPList xmlns="http://www.sigvalue.com/acc">
<UserData>
<senderId>string</senderId>
...

关于java - 在有效的 SOAP SAAJ 信封中使用默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17577207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com