在Java中格式化XML命名空间

时间:2017-01-17 12:23:38

标签: 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:Response>
  </soapenv:Body>
</soapenv:Envelope>

我们的Java实现产生以下XML:

<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>

他们不想在XML中使用连字符。

以下是产生返回值的代码:

        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
            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();

有人可以引导我们如何调整代码以返回他们期望的格式吗?

2 个答案:

答案 0 :(得分:0)

您应该阅读此topic

如果你想真正改变前缀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");
    }

}

答案 1 :(得分:0)

您需要设置前缀。您发布评论的代码已经评论了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();