机器人,帮助有simpleframework的PersistenceException机器人、simpleframework、PersistenceException

2023-09-06 19:13:03 作者:Waive.(放弃)

我想使用 org.simpleframework.xml 。类来处理我的Andr​​oid项目的XML数据。我不明白如何建立我的班点来构造器的XML定义相符:在运行时我得到这个异​​常:

  org.simpleframework.xml.core.PersistenceException:构造不类koine.marcos.wifidemo.Point匹配 

我的XML数据是这样的:

文件points.xml:

 <?XML版本=1.0编码=UTF-8&GT?;<&点GT;   <点ID =拉焦孔达>      < RSSI SSID =beacon1BSSID =00:21:91:D1:36:62> -52< / RSSI>      < RSSI SSID =beacon2BSSID =00:12:A9:03:23:32> -97< / RSSI>   < /点>   <点ID =香格里拉圣母怜子图>      < RSSI SSID =beacon1BSSID =00:21:91:D1:36:62> -68 LT; / RSSI>      < RSSI SSID =beacon2BSSID =00:12:A9:03:23:32> -83< / RSSI>   < /点>< /点> 

文件Rssi.java:

  @root公共类RSSI {    @Attribute(必填= FALSE)    保护字符串ID;    @Element(必填= FALSE)    保护的整数值;    ... getter和setter ...} 
A Simple Framework for Contrastive Learning of Visual Representations

文件point.java:

  @root公共类点{    @属性    保护字符串ID;    @ElementMap(进入=RSSI,重点=ID,属性=假,                内嵌= TRUE,所需= FALSE)    私人地图<字符串,整数> rssiMap;    公共点(串ID,地图<字符串,整数> RSSI){        this.id = ID;        ...    }    ...} 

文件要点:java的:

  @Element公共类点{    @ElementList(内嵌= TRUE,所需= TRUE)    私人列表<点和GT;清单;    ... getter和setter ...} 

解决方案

好了,因为我一直的坚定倡导者如何真棒简单的XML真的是我认为我会给你一个完整的回答这个问题,所以在这儿呢。工作完全code。

Points.java

  //您可以随意这种非私有,更复杂。公共类点{    @ElementList(进入=点,内联= TRUE)公开的ArrayList<点和GT;点;} 

Point.java

 公共类点{    私人最终字符串ID;    私人最终的HashMap<字符串,整数> rssiMap;    公共点(@Attribute(NAME =ID)字符串ID,@ElementMap(属性=真,进入=RSSI,重点=SSID,VALUETYPE = Integer.class,内嵌= TRUE)的HashMap<字符串,整数> rssiMap){        this.id = ID;        this.rssiMap = rssiMap;    }    @Attribute(NAME =ID)    公共字符串的getId(){            返回ID;    }    @ElementMap(属性=真,进入=RSSI,重点=SSID,VALUETYPE = Integer.class,内嵌= TRUE)    公众的HashMap<字符串,整数> getRssi(){            返回rssiMap;    }} 

Main.java

 公共类主要{    公共静态无效的主要(字串[] args)抛出异常{        串行串行=新的持留();        //警告:您将需要确保该文件存在或改变它。        档案文件=新的文件(数据/ data.xml中);        点积分= serial.read(Points.class,文件);        对于(点对点:points.points){            的System.out.println(point.getId());            对于(进入<字符串,整数>进入:point.getRssi()的entrySet()){                的System.out.println(+ entry.getKey()+:+ entry.getValue());            }        }    }} 

这是所有有给它。它应该很容易在你的数据读取。如果你要试验,code,那么你必须确保的唯一事情是,主要功能正确设置文件,你会读或你只要给读功能正确输入。

P.S。所以我知道它的工作原理我已经在我的电脑上测试这一点。干杯。

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 ...
}

解决方案

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

// You can make this non private and more complex at will.
public class Points {
    @ElementList(entry = "point", inline = true) public ArrayList<Point> points;
}

Point.java

public class Point {
    private final String id;
    private final HashMap<String, Integer> rssiMap;

    public Point(@Attribute(name = "id") String id, @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true) HashMap<String, Integer> rssiMap) {
        this.id = id;
        this.rssiMap = rssiMap;
    }

    @Attribute(name = "id") 
    public String getId() {
            return id;
    }

    @ElementMap(attribute = true, entry = "rssi", key = "ssid", valueType = Integer.class, inline = true)
    public HashMap<String, Integer> getRssi() {
            return rssiMap;
    }
}

Main.java

public class Main {
    public static void main(String[] args) throws Exception {
        Serializer serial = new Persister();

        // Warning: You will need to make sure that this file exists or change it.
        File file = new File("data/data.xml");
        Points points = serial.read(Points.class, file);
        for(Point point : points.points) {
            System.out.println(point.getId());
            for(Entry<String, Integer> entry : point.getRssi().entrySet()) {
                System.out.println(" " + entry.getKey() + ": " + entry.getValue());
            }
        }
    }
}

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.