Android,帮助解决 simpleframework PersistenceException
我正在尝试使用org.simpleframework.xml
。在我的 Android 项目中处理 xml 数据的类。我无法理解如何构建我的类“Point”构造函数以匹配 xml 定义:在运行时我收到此异常:
org.simpleframework.xml.core.PersistenceException: Constructor not matched for class koine.marcos.wifidemo.Point
我的 xml 数据如下所示:
文件points.xml:
<?xml version="1.0" encoding="utf-8"?>
<points>
<point id="La Gioconda">
<rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-52</rssi>
<rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-97</rssi>
</point>
<point id="La Pietà">
<rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-68</rssi>
<rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-83</rssi>
</point>
</points>
文件Rssi.java:
@Root
public class Rssi {
@Attribute(required=false)
protected String id;
@Element(required=false)
protected Integer value;
... getters and setters ...
}
文件point.java :
@Root
public class Point {
@Attribute
protected String id;
@ElementMap(entry="rssi", key="id", attribute=false,
inline=true, required=false)
private Map<String,Integer> rssiMap;
public Point(String id, Map<String,Integer>rssi) {
this.id = id;
...
}
...
}
文件点:java:
@Element
public class Points {
@ElementList(inline=true, required=true)
private List<Point> list;
... getters and setters ...
}
I am trying to use org.simpleframework.xml
. classes to handle xml data on my Android project. I can't understand how to build my class "Point" contructor to match the xml definition: at run-time I get this exception:
org.simpleframework.xml.core.PersistenceException: Constructor not matched for class koine.marcos.wifidemo.Point
My xml data is like this:
File points.xml:
<?xml version="1.0" encoding="utf-8"?>
<points>
<point id="La Gioconda">
<rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-52</rssi>
<rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-97</rssi>
</point>
<point id="La Pietà">
<rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-68</rssi>
<rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-83</rssi>
</point>
</points>
File Rssi.java:
@Root
public class Rssi {
@Attribute(required=false)
protected String id;
@Element(required=false)
protected Integer value;
... getters and setters ...
}
File point.java:
@Root
public class Point {
@Attribute
protected String id;
@ElementMap(entry="rssi", key="id", attribute=false,
inline=true, required=false)
private Map<String,Integer> rssiMap;
public Point(String id, Map<String,Integer>rssi) {
this.id = id;
...
}
...
}
File Points:java:
@Element
public class Points {
@ElementList(inline=true, required=true)
private List<Point> list;
... getters and setters ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,因为我一直是 坚定地宣扬 Simple XML 到底有多棒 我想我会给您这个问题的完整答案,所以就在这里。完整的工作代码。
Points.java
Point.java
Main.java
这就是它的全部内容。它应该很容易读取您的数据。如果您要尝试该代码,那么您必须确保的唯一一件事是 Main 函数正确设置您要读取的文件,或者您只需为读取函数提供正确的输入。
PS 我已经在我的电脑上测试过这个,所以我知道它有效。干杯。
Okay, so because I have been a firm advocate of how awesome Simple XML really is I thought that I would give you a complete answer to this question and so here it is. Completely working code.
Points.java
Point.java
Main.java
And that is all that there is to it. It should easily read in your data. If you are going to trial that code then the only thing that you must make sure of is that the Main function sets the File correctly that you are going to read from or you just give the read function the right input.
P.S. I have tested this on my computer so I know that it works. Cheers.