How can I get rid of the class attribute and perform my own conversion using XStream?
First of all I have read link text and it does not solve my problem.
I am using XStream with aliasing.
Condition is an interface with several different implementations. I want to remove the class attribute. I know that XStream uses the class attribute when it the implementing class is ambiguous and I can setup an alias for at most one of the implementing classes. I want to remove the class attribute all together and use type attribute plus the presence of other child elements to determine the implementing class. Is this possible?
Here is the XML I have currently:
<NextDestinations>
<Connector>
<DestinationId>2</DestinationId>
</Connector>
<Connector>
<DestinationId>3</DestinationId>
<condition class="com.orchestral.rhapsody.configuration.filter.condition.JavaScriptCondition" type="JAVASCRIPT">
...
</condition>
</Connector>
<Connector>
<DestinationId>4</DestinationId>
<condition class="com.orchestral.rhapsody.configuration.filter.condition.MessageTypeCondition">
<type>CONDITIONAL</type>
...
</condition>
</Connector>
<Connector>
<DestinationId>5</DestinationId>
<condition class="com.orchestral.rhapsody.configuration.filter.condition.MessageTypeCondition">
<type>CONDITIONAL</type>
...
</condition>
</Connector>
<Connector>
<DestinationId>6</DestinationId>
<condition class="com.orchestral.rhapsody.configuration.filter.condition.PropertyCondition">
<type>CONDITIONAL</type>
...
</condition>
</Connector>
<Connector>
<DestinationId>7</DestinationId>
<condition class="com.orchestral.rhapsody.configuration.filter.condition.FieldCondition">
<type>CONDITIONAL</type>
...
</condition>
</Connector>
<noMatchDestination>8</noMatchDestination>
<errorDestination>9</errorDestination>
</NextDestinations>
Here is the XML I want:
<NextDestinations>
<Connector>
<DestinationId>1</DestinationId>
</Connector>
<Connector>
<DestinationId>2</DestinationId>
<condition type="JAVASCRIPT">
...
</condition>
</Connector>
<Connector>
<DestinationId>3</DestinationId>
<condition type="CONDITIONAL">
...
</condition>
</Connector>
<Connector>
<DestinationId>4</DestinationId>
<condition type="CONDITIONAL"
...
</condition>
</Connector>
<Connector>
<Destinatio开发者_C百科nId>5</DestinationId>
<condition type="CONDITIONAL">
...
</condition>
</Connector>
<Connector>
<DestinationId>6</DestinationId>
<condition type="CONDITIONAL">
...
</condition>
</Connector>
<Connector>
<DestinationId>7</DestinationId>
<condition type="CONDITIONAL"
...
</condition>
</Connector>
<noMatchDestination>8</noMatchDestination>
<errorDestination>9</errorDestination>
</NextDestinations>
I managed to solve the class attribute problem by aliasing the type of the interface [xstream.aliasType(...)], however it then made it impossible to deserialise as it obviously cannot create an instance of the interface and having a default class was not appropriate.
The final solution was to create custom converters which was surprisingly easy.
Depending on how tied you are to XStream, the following approach using MOXy JAXB has been useful to people:
- Java/JAXB: Unmarshall Xml to specific subclass based on an attribute
精彩评论