2013-02-03 116 views
1

我希望得到一個消息,看起來像這樣:在SOAPBodyElement上刪除Body屬性/前綴,我該怎麼辦?

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

,但得到的消息,看起來像這樣(要擺脫的xmlns =「的」 subscriberCreate後):

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

更改此行? 'SOAPBodyElement element = body.addBodyElement(env.createName(「createSubscriber」));' –

+0

想要擺脫文本** xmlns =「」**!但是,無論如何感謝 – user1715352

回答

0

編輯: '?'

我想你的代碼,你就錯過了報價和德在您的交流字符串中:

String stdEncode = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; 
+0

不,還是一樣的問題。正如我所評論的,想要在文本** subscriberCreate ** – user1715352

+0

之後擺脫文本xmlns =「」我已經在API中讀取了方法addChildElement嵌入了任何範圍內的命名空間。我能解決這個問題嗎? – user1715352

+0

我認爲問題在於你試圖實現的是違反XML規範:/ –

相關問題