在Java中格式化XML名称空间
我们正在回复的API需要以下XML:在Java中格式化XML名称空间,java,xml,soap,Java,Xml,Soap,我们正在回复的API需要以下XML: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://blah.blah.blah"> <soapenv:Header/> <soapenv:Body> <api:Response>Result received successfully</api:Res
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://blah.blah.blah">
<soapenv:Header/>
<soapenv:Body>
<api:Response>Result received successfully</api:Response>
</soapenv:Body>
</soapenv:Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<api:Response>Result received successfully</api:Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
有人能告诉我们如何调整代码以返回他们期望的格式吗?您应该阅读此文章
如果确实要更改前缀SOAP-ENV,请尝试以下代码:
package projetPourTest;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.junit.Test;
public class SoapMessageTest {
@Test
public void test() throws Exception {
MessageFactory messageFactory;
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
//String SOAP_PREFIX = "soapenv";//yamin
String apiURI = "http://blah.blah.blah";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
System.out.println(envelope.getPrefix());
envelope.setPrefix("soap");
System.out.println(envelope.getPrefix());
envelope.addNamespaceDeclaration("api", apiURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyRequestElem = soapBody.addChildElement("Response", "api");
soapBodyRequestElem.addTextNode("Result received successfully");
/* Print the request message */
System.out.println("Request SOAP Message Starting at : " + System.currentTimeMillis());
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
soapMessage.writeTo(byteOutStream);
String reqmessage = new String(byteOutStream.toByteArray());
System.out.println (reqmessage);
fail("Not yet implemented");
}
}
您需要设置前缀。您发布的代码已对yamin的部分进行了注释,这将起作用
MessageFactory messageFactory;
try {
messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
//String SOAP_PREFIX = "soapenv";//yamin
String apiURI = "http://schemas.xmlsoap.org/soap/envelope/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("api", apiURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyRequestElem = soapBody.addChildElement("Response", "api");
soapBodyRequestElem.addTextNode("Result received successfully");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", apiURI + "api");
///envelope.setPrefix(SOAP_PREFIX);//yamin
// Setting the prefixes
String SOAP_PREFIX = "soapenv";//yamin
envelope.setPrefix(SOAP_PREFIX);
soapMessage.getSOAPHeader().setPrefix(SOAP_PREFIX);
soapBody.setPrefix(SOAP_PREFIX);
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message Starting at : " + System.currentTimeMillis());
soapMessage.writeTo(System.out);
System.out.println();
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
soapMessage.writeTo(byteOutStream);
String reqmessage = new String(byteOutStream.toByteArray());
System.out.println (reqmessage);
writer.append(reqmessage);
writer.close();
只要名称空间是正确的,收件人就不应该关心您使用的前缀。但是,绑定到前缀“api”的名称空间在示例输出中是错误的:这只是一个输入错误吗?
MessageFactory messageFactory;
try {
messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
//String SOAP_PREFIX = "soapenv";//yamin
String apiURI = "http://schemas.xmlsoap.org/soap/envelope/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("api", apiURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyRequestElem = soapBody.addChildElement("Response", "api");
soapBodyRequestElem.addTextNode("Result received successfully");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", apiURI + "api");
///envelope.setPrefix(SOAP_PREFIX);//yamin
// Setting the prefixes
String SOAP_PREFIX = "soapenv";//yamin
envelope.setPrefix(SOAP_PREFIX);
soapMessage.getSOAPHeader().setPrefix(SOAP_PREFIX);
soapBody.setPrefix(SOAP_PREFIX);
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message Starting at : " + System.currentTimeMillis());
soapMessage.writeTo(System.out);
System.out.println();
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
soapMessage.writeTo(byteOutStream);
String reqmessage = new String(byteOutStream.toByteArray());
System.out.println (reqmessage);
writer.append(reqmessage);
writer.close();