【问题标题】:Castor Mapping ArraysCastor 映射阵列
【发布时间】:2010-03-05 21:31:54
【问题描述】:

我有一个有构造函数的类:

public ContEmpirical(double xs[], double fs[]) {
    Check.check(xs.length == fs.length + 1 && fs.length > 0,
            "Empirical distribution array mismatch");
    this.xs = new double[xs.length];
    this.xs = xs;
    this.cs = new double[xs.length];
    double fTotal = 0.0;
    for (int i = 0; i < fs.length; i++)
        fTotal += fs[i];
    cs[0] = 0;
    for (int i = 0; i < fs.length; i++)
        cs[i + 1] = cs[i] + fs[i] / fTotal;
}

属性:

private double xs[], cs[];
    private double fs[]; // this attribute i added to make castors life easier since it always wants to map constructor arg to class attribute.

我的映射文件是:

<class name="tools.ContEmpirical">
        <description xmlns="">
            Mapping for class tools.ContEmpirical
        </description>

        <map-to xml="ContEmpirical"/>

        <field name="xs" type="double" collection="array" set-method="%1" get-method="getXs" required="true">
          <bind-xml node="attribute"/>
        </field>

        <field name="fs" type="double" collection="array" set-method="%2" get-method="getFs" required="true">
          <bind-xml node="attribute"/>
        </field></class>

然而,当我尝试编组 ContEmpirical 的实例时,我得到了这个 XML:

<ContEmpirical xs="0.2 0.3 0.4 0.8"/>

当我真的应该得到这样的东西时:

<ContEmpirical xs="0.2 0.3 0.4 0.8" fs="0.2 0.3 0.4 0.8"/>

我在映射中有什么遗漏吗?

【问题讨论】:

    标签: java xml marshalling castor


    【解决方案1】:

    你能发布 ContEmpirical 课程吗?您是否将 fs 数组初始化为字段级别的内容?

    更新:我一定错过了什么。您说您在输出 xml 中“期望” fs。但是该类是否有一个名为 fs 的属性?从您发布的构造函数代码中,fs 从未在内部分配(虽然有一个名为 fs 的参数用于计算 cs)。

    所以真的,你的对象只有一个 xs 和 cs 变量,如果你在你的 xml 中声明了 cs 的映射,你应该得到 cs。

    【讨论】:

    • 看第二个代码简介,我用getter和setter把fs做成了一个属性。
    猜你喜欢
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2021-06-26
    相关资源
    最近更新 更多