2017-05-31 52 views
0

這是我第一次處理SOAP請求和XML,所以我可能會漏掉一些明顯的東西。我不能在SOAP元素中顯示幾個名稱空間中的一個。我需要這樣的:具有多個命名空間的Java SOAPElement

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
     <cuns:HeaderInfo xmlns:cuns="http://website.com/cuns"> 
      <cuns:Field1>123456</cuns:Field1> 
      <cuns:Field2>987654321</cuns:Field2> 
     </cuns:HeaderInfo> 
    </soap:Header> 
    <soap:Body> 
     <n1:BodyField1 
      xsi:schemaLocation="http://website.xsi/location" 
      xmlns:cuns="http://website.com/cuns" 
      xmlns:n1="http://website.com/n1" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <Transaction> 
       ... 
      </Transaction> 

但我的輸出就我這裏面缺少的xmlns:XSI儘管有是XSI:上的schemaLocation BodyField1。

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
     <cuns:HeaderInfo xmlns:cuns="http://website.com/cuns"> 
      <cuns:Field1>123456</cuns:Field1> 
      <cuns:Field2>987654321</cuns:Field2> 
     </cuns:HeaderInfo> 
    </soap:Header> 
    <soap:Body> 
     <n1:BodyField1 
      xmlns:cuns="http://website.com/cuns" 
      xmlns:n1="http://website.com/n1" 
      xsi:schemaLocation="http://website.xsi/location"> 
      <Transaction> 
       ... 
      </Transaction> 

的XMLNS:XSI減速工作正常的信封和關鍵用途提名和N1出現BodyField1下。我的代碼顯式聲明xmlns:xsi然後xsi:schema。我不知道爲什麼它會顯示2個其他名稱空間和模式,但不是xsi命名空間。我嘗試了命名空間的不同順序,但它似乎並不重要。這裏是我BodyField1的代碼:

public static void makeTransaction(Vector<Transaction> transactions, SOAPMessage message){ 

    DOMSource source = null; 
    Element superRoot = null; 
    SOAPBodyElement bodyRoot = null; 
    SOAPEnvelope envelope = null; 
    SOAPBody body = null; 

    try { 
     //Make the document 
     envelope = message.getSOAPPart().getEnvelope(); 
     body = envelope.getBody(); 

     Name n1 = envelope.createName("BodyField1", "n1", 
     "http://website.com/n1"); 
     bodyRoot = body.addBodyElement(n1); 

     bodyRoot.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
     bodyRoot.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", 
     "http://schemas.xmlsoap.org/soap/envelope/SoapEnvelope.xsd"); 
     bodyRoot.addNamespaceDeclaration("cuns", "http://website.com/cuns"); 

    } catch (SOAPException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 
} 

回答

0

儘管如此,最後的答案可以幫助其他人。我正在尋找類似問題的解決方案,在我的情況下,xmlsns:xsi即將到來,但不是xsi:schemalocation。下面爲我​​做了訣竅。

SOAPElement eleXSINs = envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance"); 
eleXSINsfor .setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "http://xxxxxx.xsd");