我想收到如下所示的消息:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns="http://website.com/">
<S:Header/>
<S:Body>
<subscriberCreate>
<assignedId>Some ID</assignedId>
</subscriberCreate>
</S:Body>
</S:Envelope>
但是收到一条如下所示的消息(想要在subscriberCreate之后删除xmlns=""):
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns="http://website.com/">
<S:Header/>
<S:Body>
<subscriberCreate xmlns="">
<assignedID>Some ID</assignedID>
</createSubscriber>
</S:Body>
</S:Envelope>
有人知道如何解决这个问题吗? body 元素是否继承了信封的属性,因为当我更改它们的顺序时,消息消失了! 谢谢!
我的java代码如下所示:
import java.io.FileOutputStream;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
public class CreateSubscriber {
public static void main(String[] args) {
try{
SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPEnvelope env = sm.getSOAPPart().getEnvelope();
env.setPrefix("S");
env.removeNamespaceDeclaration("env");
sm.getSOAPHeader().setPrefix("S");
SOAPBody body = sm.getSOAPBody();
body.setPrefix("S");
SOAPBodyElement element = body.addBodyElement(env.createName("createSubscriber"));
env.setAttribute("xmlns","http://psm.proceranetworks.com/soap/3.1/message");
element.addChildElement("assignedID").addTextNode("Some ID");
FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
String stdEncode = "<xml version= 1.0 encoding= utf-8>";
System.out.print(stdEncode);
sm.writeTo(System.out);
fOut.write(stdEncode.getBytes());
sm.writeTo(fOut);
System.out.println();
System.out.println("SOAP msg created");
}catch(Exception e){
e.printStackTrace();
}
}
}
最佳答案
编辑:
我尝试了你的代码,但你缺少引号和“?”在你的编码字符串中:
String stdEncode = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
关于java - 删除 SOAPBodyElement 上的 Body 属性/前缀,我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14673512/