1

次のようなメッセージを取得したい:

<?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();
        }
    }
}
4

1 に答える 1

0

編集:

私はあなたのコードを試しましたが、引用符と de '?' がありません。あなたのエンコーディング文字列で:

String stdEncode = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
于 2013-02-03T17:50:30.223 に答える