我正在尝试通过 SOAP 将 XML 文档发送到需要有效负载 XML 包含 XML 序言的服务器,例如 <?xml version="1.0" encoding="utf-8" ?>
。以下与我正在使用的代码非常相似:
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = getSoapMessage().getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
soapHeader = envelope.getHeader();
soapBody = envelope.getBody();
soapHeader.detachNode();
SOAPBodyElement bodyElement = soapBody.addBodyElement(getEnvelope().createName("my_element_name"));
bodyElement.addAttribute(getEnvelope().createName("my_attribute_name"), "my_attribute_value");
bodyElement.addChildElement("my_child_element");
...
最佳答案
XML 序言只能存在于 XML 文档的开头。
来自此处提供的 XML 规范:http://www.w3.org/TR/2000/REC-xml-20001006#sec-prolog-dtd
The document type declaration must appear before the first element in the document.
因此,在 SOAP 文档中间编写序言是无效的 - 我怀疑您是否会找到允许您这样做的 SOAP 库或实现。您和/或您的“客户”可能误解了要求。
您可以尝试 XML validator 软件(例如 http://www.w3schools.com/xml/xml_validator.asp ),它应该报告您预期结果的错误。
关于java - 如何在 javax.xml.soap.SOAPBody 中添加 XML 序言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25170185/