集合中的Xstream绝对引用

时间:2014-08-11 05:43:30

标签: java xstream pojo

如果列表中的某个子节点引用了此列表中位于下方的其他子节点,如何通过使用XStream从XML获取对象。 尝试:

<projectEstimate id="586">
    <projectEstimateKosgus class="list">

      <projectEstimateKosgu>
        <amount>1.00</amount>
        <parent class="projectEstimateKosgu" reference="/projectEstimate/projectEstimateKosgus/projectEstimateKosgu[2]"/>
        <estimate class="projectEstimate" reference="/projectEstimate"/>
      </projectEstimateKosgu>

      <projectEstimateKosgu>
        <amount>2.00</amount>
        <estimate class="projectEstimate" reference="/projectEstimate"/>
      </projectEstimateKosgu>

    </projectEstimateKosgus>
</projectEstimate>

projectEstimate对象具有集合字段“projectEstimateKosgus”,此集合的子节点具有字段“parent”,该字段引用此集合中的“projectEstimateKosgu”对象。当我尝试在标记中设置父级时,它位于引用标记的上方,我得到:

com.thoughtworks.xstream.converters.ConversionException: Invalid reference

但是,如果改变“projectEstimateKosgu”标签的顺序,就像这样

<projectEstimate id="586">
    <projectEstimateKosgus class="list">

      <projectEstimateKosgu>
        <amount>2.00</amount>
        <estimate class="projectEstimate" reference="/projectEstimate"/>
      </projectEstimateKosgu>

      <projectEstimateKosgu>
        <amount>1.00</amount>
        <parent class="projectEstimateKosgu" reference="/projectEstimate/projectEstimateKosgus/projectEstimateKosgu"/>
        <estimate class="projectEstimate" reference="/projectEstimate"/>
      </projectEstimateKosgu>

    </projectEstimateKosgus>
</projectEstimate>

1 个答案:

答案 0 :(得分:0)

您可以将relative absolute mode用于XStream对象,方法是将其模式设置为:

  xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

或者您可以将ID mode用于XStream对象,方法是将其模式设置为:

  xstream.setMode(XStream.ID_REFERENCES);

Reference