2012-02-08 208 views
0

我試圖發送SOAP消息。發送SOAP消息

我加入手動頭的消息,使用下面的代碼:

public static void main(String[] args) { 
    try{ 
     AmdocsServicesServiceagentLocator locator = new AmdocsServicesServiceagentLocator(); 
     PortTypeEndpoint1BindingStub port = new PortTypeEndpoint1BindingStub(new URL("http://srvp7rd-tibco.rnd.local:8025/Process/SoapRequests/Amdocs-Services.serviceagent/PortTypeEndpoint1"),locator); 
     GetContactRelatedInfo parameters = new GetContactRelatedInfo(); 
     GetContactRelatedInfoRequest request = new GetContactRelatedInfoRequest(); 
     request.setPersonID("6610782925"); 
     request.setPersonIDType("ID number (CPR)"); 

     /* Creating an empty XML Document - We need a document*/ 

     DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); 
     Document doc = docBuilder.newDocument(); 

     /* Creating the XML tree */ 

     /* Create the root element and add it to the document */ 
     Element root = doc.createElement("mul:MultiTenant"); 
     doc.appendChild(root); 

     /* Adding the child to the root */ 
     Element child = doc.createElement("mul:OpCo"); 
     root.appendChild(child); 

     /* Add text element to the child */ 
     Text text = doc.createTextNode("DENMARK"); 
     child.appendChild(text); 

     /* Adding the child to the root */ 
     child = doc.createElement("mul:BS"); 
     root.appendChild(child); 

     /* Add text element to the child */ 
     text = doc.createTextNode("ENV3"); 
     child.appendChild(text); 

     SOAPHeaderElement element = new SOAPHeaderElement("" ,"soapenv:Header" , doc); 
     element.setActor(null); 
     port.setHeader(element); 
     System.out.println(port.getHeaders()[0]); 
     port.getContactRelatedInfoOperation(parameters); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

但我不知道爲什麼,或者我是如何結束了包括我沒有」屬性的消息不想要。 例如當前代碼的輸出信息是:

<soapenv:Header soapenv:mustUnderstand="0" xsi:type="ns1:Document" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ns1="http://xml.apache.org/xml-soap"> 
<mul:MultiTenant xmlns:mul=""> 
      <mul:OpCo xmlns:mul="">DENMARK</mul:OpCo> 
      <mul:BS xmlns:mul="">ENV3</mul:BS> 
</mul:MultiTenant></soapenv:Header> 

例如,在mul:OpCo標籤的xmlns:mul=""屬性。 有沒有辦法刪除該屬性?

回答

1

那些不是屬性,那些是名稱空間聲明。您正在使用mul:命名空間前綴創建元素,並且必須在某處定義該前綴。 Java正在添加一個默認的空聲明(xmlns:mul=""),以便您的XML最終形成格式 - 您無需聲明即可使用前綴。

如果您不想要這些聲明,請刪除mul:前綴,或在文檔的其他地方正確定義它。儘管如此,你還沒有告訴我們你的文檔應該是什麼樣子,所以很難告訴你如何做到這一點。

+0

頭看起來應該是: < - 可選:! - > ?可選: - > 2012-02-08 14:55:21

+1

@Tal:這不是有效的XML。你*必須*聲明'mul'命名空間。 – skaffman 2012-02-08 15:10:44

+0

那麼如何在不必處理命名空間的情況下添加「mul」呢? – 2012-02-08 15:10:53

1

您的消息沒有聲明mul命名空間。添加它,奇怪的xmlns:mul屬性應該消失。

更新

現在我明白了,你只需要創建一個SOAP消息的片段。這只是頭部,並且mul名稱空間可以在另一個SOAP-Envelope元素上聲明。在soapUI的

需要知道的mul命名空間(-name),仔細檢查完整的SOAP消息,並仔細檢查文檔。然後在doc上聲明命名空間。稍後,如果外部元素以完全相同的方式聲明mul,則這些屬性應該從序列化的xml中消失。

+0

標題應該如下所示: <! - 可選: - > ?可選: - > 2012-02-08 14:53:18

+0

那麼如何在不必處理名稱空間的情況下添加「mul」呢? – 2012-02-08 15:11:30