Simple-xml:如何注释我的“点”类

时间:2011-05-03 08:15:49

标签: java android simple-framework

我必须在Android程序中表示这样的数据结构:

<?xml version="1.0" encoding="utf-8"?>
<points>
    <point id="La Gioconda">
       <rssi id="north">-55</rssi>
       <rssi id="east" >-76</rssi>
       <rssi id="south">-64</rssi>
       <rssi id="east" >-92</rssi>
    </point>
    <point id="La Pietà">
       <rssi id="north">-51</rssi>
       <rssi id="east" >-60</rssi>
       <rssi id="south">-88</rssi>
       <rssi id="east" >-59</rssi>
    </point>
</points>

下面我报告了我使用的Point,Point和Rssi类,得到了这个错误:

05-03 07:49:19.884: DEBUG/com.marcos.training.TrainingActivity(354): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=, required=true, type=void) on field 'dbm' protected java.lang.Integer com.marcos.training.Rssi.dbm for class com.marcos.training.Rssi at line 4

我肯定会错过一些明显的东西......: - (

File Points.java:

package com.marcos.training;

import java.util.List;

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

@Element
public class Points {
    @ElementList(inline=true)
    private List<Point> list;

    public Integer getSize() {
        return list.size();
    }

    public List<Point> getList() {
       return list;
    }
}

File Point.java:

package com.marcos.training;

import java.util.List;

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

import android.util.Log;

@Root
public class Point {
    private final static String TAG = TrainingActivity.class.getCanonicalName();

    @ElementList(inline=true)
    private List<Rssi> list;

    @Attribute
    protected String id;

    public String getId() {
        return id;
    }

    public Integer getRssi(String id) {
        int len = list.size();
        for (int i = 0; i < len; i++) {
            if (list.get(i).getId().equalsIgnoreCase(id)) {
                return list.get(i).getDbm();
            }
        }
        Log.d(TAG, "getRssi(): unforeseen id \"" + id + "\"");
        return 999;
    }
}

文件Rssi.java:

package com.marcos.training;

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

@Root
public class Rssi {

    @Attribute
    protected String id;

    @Element
    protected Integer rssi;

    public String getId() {
       return id;
    }

    public Integer getRssi() {
      return rssi;
   }
}

2 个答案:

答案 0 :(得分:6)

问题在于您的Rssi课程。 rssi元素没有子元素,因此以下注释不正确

@Element
protected Integer rssi;

您需要使用@Text注释:

@Text
protected Integer rssi;

或者,最好使用ElementMap来保存rssi映射,而不是列表。例如,您可以将Point课程更改为:

@Root
public class Point {

    @ElementMap(entry="rssi", key="id", attribute=true, inline=true)
    private Map<String,Integer> rssiMap;

    @Attribute
    protected String id;

    public String getId() {
        return id;
    }

    public Integer getRssi(String id) {
        Integer val = rssiMap.get(id);
        if(val == null){
            return 999;
        }
        return val;
    }
}

答案 1 :(得分:2)

首先,您应该从Rssi和Point类中删除@Root,因为它们不是xml的根。

其次,你显然在这里有类或xml,因为错误与某些“dbm”字段有关,在xml和java代码中遗漏了这个字段。我假设,在较新的版本中你有类似的东西:

 <rssi id="north" dbm="55"/>

然后您需要的只是在Rssi课程中:

  @Attribute
  private Integer dbm;