【问题标题】:XStream issue with implicit collections隐式集合的 XStream 问题
【发布时间】:2013-04-22 06:21:51
【问题描述】:

我有以下 XML:

<appConfig>
    <database isSource="true" fizz="129"/>
    <database isSource="false" fizz="29494" />
    <buzz bee="always" />
</appConfig>

这应该映射到以下 POJO:

public class AppConfig {
    private Database sourceDb;
    private Database targetDb;
    private Buzz buzz;
}

public class Database {
    // Omitted for brevity...
}

public class Buzz {
    // Omitted for brevity...
}

并且已经配置了以下XStreammapper:

XStream oxMapper = new XStream();
oxMapper.alias("appConfig", AppConfig.class);
oxMapper.alias("database", Database.class);
oxMapper.alias("buzz",  Buzz.class);

像这样读取和反序列化 XML:

ApplicationConfig appCfg = (ApplicationConfig)oxMapper.fromXML(
        getXMLSnippet());

但是我得到了这个错误:

Element database of type com.myapp.Database is not defined as field in type com.myapp.AppConfig

我读到 this article 谈到隐式集合,相信 XStream 认为我的两个 Database 项目是列表的一部分(隐式集合),而实际上它们不是(它们应该映射到 2 个不同的 AppConfig 属性)。

我该如何解决这个问题?提前致谢!

【问题讨论】:

    标签: java xml xstream oxm


    【解决方案1】:

    您必须通过字段的名称设置别名,而不是类型。 对于您的示例,它应该是:

    oxMapper.alias("sourceDb", Database.class);
    oxMapper.alias("targetDb", Database.class);
    oxMapper.alias("buzz",  Buzz.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2011-08-07
      • 2011-03-09
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多