October 21, 2024
Chicago 12, Melborne City, USA
java

Disambiguate XStreamAlias elements through xpath


I’m trying to convert a FIX XML dictionary to an object model using Xstream. The @XStreamAlias mechanism works, however, is there a way to use a different Java class for XML elements with the same name but at a different position in the tree (a different xpath).

For example ‘field’ elements exists at /fix/header/field and have only two attributes (‘name’ and ‘required’):

<fix major="4" minor="4">
<header>
    <field name="BeginString" required="Y"/>
    <field name="BodyLength" required="Y"/>

But ‘field’ elements also exist at /fix/fields/field and have different attributes (‘number’, ‘name’ and ‘type’):

    ...
    <fields>
    <field number="1" name="Account" type="STRING"/>
    <field number="2" name="AdvId" type="STRING"/>

Using an XStreamAlias class with a combination of all attributes works, but it’s not very pretty – mixing to types of field definitions into one

@XStreamAlias("field")
public static class FixFieldInfo
{
  @XStreamAsAttribute
  public String name;

  @XStreamAsAttribute
  public String required;

  @XStreamAsAttribute
  public String number;

  @XStreamAsAttribute
  public String type;

Ideally I could create one class for the first type of fields and another class for the second type of fields. Is there something like specifying the complete xpath inside the alias annotation ?

For example: @XStreamAlias("/fix/header/field") and @XStreamAlias("/fix/fields/field") would solve my problem.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video