简单的XML序列化非必填字段

时间:2017-07-19 10:41:33

标签: java android xml

当我尝试将java对象解析为xml时。我的xml看起来不太好,并且它没有关闭标签,我有一个不需要的值,我看到了这个值。

这是我的班级:

@Root(name="root")
public class Example {

    @Element(name="message" , required = false)
    private String text;

    @Attribute(name="id", required = true)
    private int index;

    @Attribute( required = false)
    private int index2;


    public String getMessage() {
        return text;
    }

    public int getId() {
        return index;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
 }

当我这样做时:

      Serializer serializer = new Persister();
      Example example1 = new Example();

      example1.setIndex(111111);
      String path = Environment.getExternalStorageDirectory() + File.separator + "yourFolder";
        File folder = new File(path);
        File file1 = new File(folder, "qqq.xml");

      try {
        serializer.write(example1, file1);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

我的输出XML如下所示:

<root index2="0" id="1

为什么我有一个index2 =&#34; 0&#34; ?而且没有id =&#34; 1&#34;吗

0 个答案:

没有答案