2011-05-10 71 views
1

我遵循預定義的WSDL(和xsd)發送和重新調用Web服務調用。發送Web服務發送Any類型的對象。我可以發送它。當我收到回覆時,我們會得到任何元素的列表。下面是代碼,我們使用:解組肥皂類拋出異常

 List<Object> list = academicRecordBatch.getBatchContent().getAny(); 
     if (list != null && list.size() > 0) { 
        Log.debug("got : "+ list.get(0).getClass().getName()); 
        K12StudentType k12StudentType = (K12StudentType) list.get(0); //error on this line 
     } 

這將產生以下錯誤: 【JAVA] 2011-05-10 09:52:53707 DEBUG [com.mycompany.is.Test]主(線42):返回的對象:[email protected]3bead5 [java] java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl無法轉換爲org.pesc.sector .academicrecord.v1_4.K12StudentType

看起來像返回的是ElementNSImpl的列表。我如何從中提取我的K12StudentType對象?
我將不勝感激任何建議。

+0

JAXB已經爲您的'batchContent'屬性生成了一個全部getAny()'getter,因爲它可能包含任何元素。這就是爲什麼你需要在進行轉換之前明確檢查元素類型。提供解組之前得到的適當的XSD部分和XML:也許會有更多有用的評論/想法。 – 2011-11-21 17:10:26

回答

1

ElementNSImpl實現了Node接口,並且這個link解釋瞭如何解組節點對象。我從來沒有嘗試過。

3

您是否正在訪問從Java客戶端返回DataSet的.NET webservice? 無論如何,試試這個: 假設變量'o'顯示爲一個ElementNSImpl對象。將其轉換爲org.w3c.dom.Node對象,然後使用DOM方法導航現在通過Node對象提供的返回XML。

import org.w3c.dom.*; // Add this import. 

Object o = objs.get(0); // the ElementNSImpl object. 
Node dataSetNode = (Node)o; 

// Some more code for illustration..    
if (dataSetNode != null) { 
Node tableNode = dataSetNode.getFirstChild(); 
if (tableNode != null) { 
Node dataElementNode = tableNode.getFirstChild(); 
     while (dataElementNode != null) { 
      String text = dataElementNode.getTextContent(); 
      String name = dataElementNode.getNodeName(); 
      System.out.format("%s: %s\n", name, text); 
      dataElementNode = dataElementNode.getNextSibling(); 
    }     
}