SimpleXML Framework返回所有嵌套元素

时间:2014-08-21 20:25:56

标签: java xml nested simple-framework

我有一个xml文档如下:

<?xml version="1.0" encoding="utf-8"?>
<Calculations>
    <desc>Calculations Description</desc>
    <Calculation operator="ADD" valueA="1" valueB="2">
        <desc>First Description</desc>
        <owners>
        <owner id="1"><name>Alan</name></owner>
        <owner id="2"><name>Karen</name></owner>
        <owner id="3"><name>Isla</name></owner>
        <owner id="4"><name>Bethany</name></owner>
        </owners>
    </Calculation>
    <Calculation operator="SUBTRACT" valueA="3" valueB="1">
        <desc>Second Description</desc>
        <owners>
        <owner id="10"><name>Christopher</name></owner>
        <owner id="11"><name>David</name></owner>
        <owner id="12"><name>Matt</name></owner>
        <owner id="13"><name>Peter</name></owner>
        </owners>
        </Calculation>
    <Calculation operator="MULTIPLY" valueA="2" valueB="5">
        <desc>Third Description</desc>
        <owners>
        <owner id="10"><name>Christopher</name></owner>
            <owner id="11"><name>David</name></owner>
            <owner id="12"><name>Matt</name></owner>
            <owner id="13"><name>Peter</name></owner>
        </owners>
                </Calculation>
    <Calculation operator="DIVIDE" valueA="2" valueB="5">
        <desc>Fourth Description</desc>
        <owners>
        <owner id="10"><name>Christopher</name></owner>
            <owner id="11"><name>David</name></owner>
            <owner id="12"><name>Matt</name></owner>
            <owner id="13"><name>Peter</name></owner>
        </owners>
                </Calculation>
</Calculations>

我试图使用以下java类反序列化它:

Calculations.java:

package Simple;

import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name="Calculations")
public class Calculations {

    @Element(name="desc")
       private String text;

    @ElementList(inline=true)
       public static List<Calculation> calculations;


       public Calculations() {
          super();
       }  
       public List<Calculation> getCalculations() {
              return calculations;
           }
        public String getDescription() {
              return text;
           }

}

Calculation.java:

package Simple;

import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name="Calculation")
public class Calculation {
    public Calculation() {
        super();
    }

    @Attribute(name="operator")
       private String operator;


    @ElementList
       public static List<Owner> owners;

    public String getOperator() {
          return operator;
       }

  public List<Owner> getOwners() {
          return owners;
       }

}

和owner.java:

package Simple;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name="Owner")
public class Owner {
    public Owner() {
        super();
    }

    @Attribute(name="id")
       private String id;

    @Element(name="name")
    private String name;

    public String getID() {
          return id;
       }

    public String getName() {
          return name;
       }
}

我已经成功运行(主要是) - 目前它只是在一个测试项目中,我正在通过几个for循环将详细信息写到控制台。

问题在于,当我打印出来时,而不是每个“计算”有4个所有者,返回的是每个计算有16个所有者 - 每个计算返回所有16个所有者,如我的控制台所示: / p>

ADD
 - Alan
 - Karen
 - Isla
 - Bethany
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
SUBTRACT
 - Alan
 - Karen
 - Isla
 - Bethany
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
MULTIPLY
 - Alan
 - Karen
 - Isla
 - Bethany
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
DIVIDE
 - Alan
 - Karen
 - Isla
 - Bethany
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
 - Christopher
 - David
 - Matt
 - Peter
end

我已经搜索了几个小时的论坛,并且还通过了SimplXML教程和示例,我根本找不到为什么会发生这种情况的线索。

我假设当“读取”过程迭代到每次计算时,它只会从该迭代中选择所有者,但由于某种原因,它会将所有这些添加到每次计算中。

我很感激你能给予的任何帮助。

谢谢,

艾伦

1 个答案:

答案 0 :(得分:0)

我建议您不要使用静态列表字段,而是使用类中的非静态或实例列表字段。


即改变这个:

@ElementList
public static List<Owner> owners;

到此:

@ElementList
public List<Owner> owners; // not static

和改变这一点相同:

@ElementList(inline=true)
public static List<Calculation> calculations;

到此:

@ElementList(inline=true)
public List<Calculation> calculations;

通过静态,类的所有实例将共享相同的List,这意味着Calculation的所有实例都将具有相同的Owners列表,这可能是您遇到问题的根源。此外,您应该强烈考虑将上面的字段设置为私有,并且只允许通过getter和setter访问。稍后您可能只想发布原始列表的深层副本,只有在限制对字段的直接访问时才能实现此目的。