Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception with Simple XML framework deserialization

I'm facing a problem to deserialize an XML file which have successfully been serialized with the Simple XML Serialization framework (simpleframework.org).

There is an exception thrown:

org.simpleframework.xml.core.PersistenceException: Constructor not matched for class projet.sarelo.Note

This is the call:

Serializer serializer = new Persister();
File xmlFile = new File(path);
ContactList contactList = serializer.read(ContactList.class, xmlFile); <== Error

My ContactList.java

@Root(strict=false, name="ContacList")
public class ContactList {      
    @ElementArray (name = "Contacts")
    Contact [] contact;     
}   

My Note.java

public class Note {
    @Element(required=false)
    private String note;

    public Note(String note) {
        super();
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

My Contact.java

@Root
public class Contact {
@Attribute(name = "id") 
public String id;       

@Element(name="Nom", required=false)                
String name; 

@ElementArray(name="Phones", required=false)
Phone [] phone; 

@ElementArray(name = "Emails", required=false)
Email [] email; 

@ElementArray(name = "Adresses", required=false)
Adresses [] adresses;

@ElementArray(name = "Notes", required=false)
Note [] note;

public Contact(String id, String name) {
    super();
    this.id = id;
    this.name = name;
}

public String getName() {
    return name;
}   

public String getId(){
    return id;
}
}

And this is the XML file I'm trying to deserialize.

<ContactList>
<Contacts length="5">
  <contact id="1">
     <Adresses length="0"/>
     <Emails length="0"/>
     <Notes length="1">
        <note>
           <note>dgfdg</note>
        </note>
     </Notes>
  </contact>
  <contact id="2">
     <Adresses length="1">
        <adresses>
           <city>Paris </city>
           <postcode>751234 </postcode>
           <state>France</state>
           <street>Pignon</street>
        </adresses>
     </Adresses>
     <Emails length="1">
        <email type="home">
           <home>[email protected]</home>
        </email>
     </Emails>
     <Nom>Nicolas  Sarkozy </Nom>
     <Notes length="1">
        <note>
           <note>Je suis le president de toute la france. Le grand president</note>
        </note>
     </Notes>
     <Phones length="2">
        <phone>
           <home>+33 1234</home>
        </phone>
        <phone>
           <mobile>+33 0612</mobile>
        </phone>
     </Phones>
  </contact>
    ...
</Contacts>
</ContactList>
like image 843
San Francesco Avatar asked Sep 19 '11 12:09

San Francesco


People also ask

What is the correct way of using XML deserialization?

The most common usage of XML serialization is when XML data is sent from the database server to the client. Implicit serialization is the preferred method in most cases because it is simpler to code, and sending XML data to the client allows the Db2 client to handle the XML data properly.

What is XML serialization and deserialization in c#?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.


2 Answers

No-Arg Constructor

I don't know this particular XML framework, but, usually you need a constructor that takes no parameters/arguments for each class that you wish to be deserialized. Such constructors are known as a "no-arg", "0-argument", or (formally) nullary constructor.

Otherwise, the framework cannot instantiate the class.

like image 149
Timores Avatar answered Oct 24 '22 20:10

Timores


You don't have to delete things from constructor. You can just add something like this:

public Contact(@Element (name = "id") String id, @Element (name = "name") String name) {
...

It was working for me :)

like image 36
Tunerx Avatar answered Oct 24 '22 20:10

Tunerx