4

EDIT: Removed previous edit

I'm trying to deserialize the following:

<?xml version="1.0" encoding="UTF-8"?>
<ALL>
    <KAMP>
        <ID>1</ID>
        <SQLTID>1376881200</SQLTID>
        <DATO>2013-08-19</DATO>
    </KAMP>
    ...
    <KAMP>
        <ID>2</ID>
        <SQLTID>1376881200</SQLTID>
        <DATO>2013-08-19</DATO>
    </KAMP>
</ALL>

Using

@Root
public class Matches {

    @ElementList
    private List<Match> list;

    public List getMatches() {
        return list;
    }
}

And

@Root(name = "KAMP", strict = false)
public class Match{

    @Element(name = "ID", required = false)
    public String Id;

    @Element(name = "SQLTID", required = false)
    public String Sqltid;

    @Element(name = "DATO", required = false)
    public String MatchDate;
}

I keep getting

Element 'KAMP' does not have a match in class <myClass>

I've tried adding (name = "KAMP") to @ElementList but it didn't help.

Can anyone else help?

CJe
  • 1,928
  • 3
  • 24
  • 53
  • Did you want it to map `Match` to `KAMP` ? You should provide "directions" ;) – Nir Alfasi Aug 19 '13 at 15:19
  • Yes @alfasin. That's what I want. To get a collection/list of Match objects from an XML containing KAMP elements. – CJe Aug 19 '13 at 15:40
  • Still nuthin :-( I CAN deserialize a single KAMP element alone in a file but not more than one wrapped in an element... must be so close... :-) – CJe Aug 19 '13 at 17:09

1 Answers1

7

The following code works for me, pay special attention to the following two items:

  1. empty constructor in ALL.java
  2. That the List has annotation of inline=true

ALL.java:

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.File;
import java.util.List;

/**
 * User: alfasin
 * Date: 8/19/13
 */
@Root(name="ALL")
public class ALL {

    @ElementList(entry="KAMP", inline=true)
    private List<KAMP> kamp;

    public ALL(){};

    public List<KAMP>  getMatches() {
        return kamp;
    }

    public static void main(String...args) throws Exception {
        Serializer serializer = new Persister();
        File example = new File("/Users/alfasin/Documents/workspace-sts-3.2.0.RELEASE/SimpleXML/src/kamp.xml");
        ALL all = serializer.read(ALL.class, example);
        for(KAMP tmp : all.getMatches()){
            System.out.println("ID: "+tmp.Id);
            System.out.println("MatchDate: "+tmp.MatchDate);
            System.out.println("Sqltid: "+tmp.Sqltid);
            System.out.println("----------");
        }
    }
}

KAMP.java

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

/**
 * User: alfasin
 * Date: 8/19/13
 */
@Root(name="KAMP",strict = false)
public class KAMP {

    @Element(name = "ID", required = false)
    public String Id;

    @Element(name = "SQLTID", required = false)
    public String Sqltid;

    @Element(name = "DATO", required = false)
    public String MatchDate;

}

OUTPUT

ID: 1
MatchDate: 2013-08-19
Sqltid: 1376881200
----------
ID: 2
MatchDate: 2013-08-19
Sqltid: 1376881200
----------
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • "Should define" as in HAVE to? :-) Or can I get around it using attributes? – CJe Aug 19 '13 at 15:28
  • @CJe you can change the name of the class if you don't want to use annotations for mapping. – Nir Alfasi Aug 19 '13 at 15:29
  • So I tried renaming my Matches class to ALL and add name = "KAMP" to my Match class - still no good: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=KAMP, inline=false, name=, required=true, type=void) on field 'list' private java.util.List – CJe Aug 19 '13 at 15:34
  • @CJe it didn't help you since ID, SQLTID and DATO should be "under" KAMP - which means that an object called KAMP should CONTAIN these properties. in your current class they live "as peers" (in parallel) – Nir Alfasi Aug 19 '13 at 15:39
  • Like I suggested on the second part of my answer: 1. change "match" to "KAMP" and then, 2. create a new class for ALL which will hold a list of KAMP (Match) objects. Make sure your objects have the same structure as your XML – Nir Alfasi Aug 19 '13 at 15:41
  • that I dont understand... ID, SQLTID and DATO *are* under KAMP in my XML. What do you mean by "as peers"? On the same level or...? – CJe Aug 19 '13 at 15:43
  • I'll try create classes that match (no pun intended) the XML elements but I really would like for my code to be in English even though the XML elements are named in Danish... – CJe Aug 19 '13 at 15:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35776/discussion-between-cje-and-alfasin) – CJe Aug 19 '13 at 15:47
  • Thanks for the update! Could you elaborate just a bit on especially the empty constructor? Why is that needed? Will give it a go when I get home. – CJe Aug 20 '13 at 07:10
  • 1
    @CJe I'm not sure why, I can only guess that it has something to do with SimpleXML implementation – Nir Alfasi Aug 20 '13 at 07:16