2013-12-22 59 views
0

我想讀這樣的XML使用XStream的對象:Xstream和使用保留關鍵字作爲XML屬性的解決方法。 (「類」)

<behavior id="id384781" class="ChangeAttributeAction" enabled="true"> 
     <attributes> 
     <text id="name">Change Attribute</text> 
     <text id="bundleName">ChangeAttribute.behavior</text> 
     <text id="targetAttribute">game.devices.touches.touch1.y</text> 
     <text id="RHS"> 
      <expression> 
      <string>acos(x)</string> 
      </expression> 
     </text> 
     </attributes> 
    </behavior> 

一切工作,因爲他們應該除了class="ChangeAttributeAction"部分。

我有我的行爲類:

@XStreamAlias("class") 
@XStreamAsAttribute 
String classAtt = ""; 

即使是艱難的,我走樣的字段名稱,它給人的錯誤:在線程「主要」 com.thoughtworks.xstream

例外。 converters.ConversionException: ChangeAttributeAction:ChangeAttributeAction

----調試信息----

消息:ChangeAttributeAction

原因的異常:com.thoughtworks.xstream.mapper.CannotResolveClassException

原因消息:ChangeAttributeAction

:projectmerger1.Behaviors

需要型:projectmerger1.Behaviors

轉換器型:com.thoughtworks.xstream.converters.reflection.ReflectionConverter

它曾經以某種方式工作,但現在沒有。我曾經以不同的方式對待對象,但我無法找到我所做的不同來實現這一點。同時在我的研究 - >link < - 我瞭解到,嘗試NO_REFERENCE模式可能會起作用,但它並沒有在我的情況。

回答

1

我解決它通過添加:

xstream.aliasSystemAttribute("", "class"); 

我不知道爲什麼它的工作原理,但它的作品。無論如何,謝謝:)

+0

一個名爲'class'的屬性對XStream有特殊的含義 - 如果你有一個具有特定聲明類型的屬性,但你爲該屬性提供的值是一個子類型,那麼XStream使用'class'來記錄價值的真實類型。這個'aliasSystemAttribute'調用禁用了這個邏輯。 –

+0

我明白了,謝謝。 – cbt

相關問題