简体   繁体   English

用Java格式化XML名称空间

[英]Formatting XML namespace in Java

The API that we are replying to expects the following XML: 我们要答复的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>

Our Java implementation produces the following XML: 我们的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>

They don't want the hyphen in the XML. 他们不需要XML中的连字符。

The following is the code that produces our return value: 以下是产生我们的返回值的代码:

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

Can anyone lead us how to tweak our code to return the format they expect? 谁能带领我们调整代码以返回他们期望的格式?

You should read this topic 您应该阅读本主题

if you want really change the prefix SOAP-ENV try this code : 如果您真的想更改前缀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");
    }

}

You need to set the prefix. 您需要设置前缀。 The code you posted commented has commented sections by yamin which would work. 您发表评论的代码对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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM