使用XStream解组为静态类型

时间:2017-12-19 13:12:18

标签: java xstream jaxb2

我有一个格式如下的XML文件:

<TRequest>
    <Item>
        <ItemIdentification>
            <ItemStandardID Qualifier="XX">egosdhgsuh</ItemStandardID>
        </ItemIdentification>
    </Item>
    <Item>
    ....
    </Item>
</TRequest>

从相关的WSLD的XSD中,我使用JaxB生成了Java代码。这给了我以下层次结构:

public class TRequest extends Request {

    protected List<TRequest.Item> item;

    public static class Item {

        protected TRequest.Item.ItemIdentification itemIdentification;

        public static class ItemIdentification {

            protected TRequest.Item.ItemIdentification.ItemStandardID itemStandardID;

            public static class ItemStandardID {

            }

        }

    }

}

我正在尝试使用Item阅读TRequest内的XStreamMarshaller

    final XStreamMarshaller unmarshaller = new XStreamMarshaller();
    final ImmutableMap<String, ?> of = ImmutableMap.of(
            "Item", TRequest.Item.class,
            "ItemIdentification", TRequest.Item.ItemIdentification.class
    );
    unmarshaller.setAliasesByType(of);

但是当我尝试解组它时,我收到以下错误:

org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.mycompany.TRequest$Item.ItemIdentification
---- Debugging information ----
field               : ItemIdentification
class               : com.mycompany.TRequest$Item
required-type       : com.mycompany.TRequest$Item
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /Item/ItemIdentification
line number         : 2
version             : 4.3.13.RELEASE
-------------------------------

在上面对setAliasesByType的调用中,我尝试了Item.ItemIdentificationItem$ItemIdentification等内容作为TRequest.Item.ItemIdentification.class的键映射。

实现这一目标的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我相信除了定义正确的类之外,还需要将这些类之间的关系定义为变量。

public class TRequest extends Request {
    Item Item;
}

public class Item {
    ItemIdentification ItemIdentification;
}


public class ItemIdentification {
    ItemStandardID ItemStandardID;
}

public class ItemStandardID {
    String Qualifier;
}

相关问题