使用简单XML框架反序列化的异常 [英] Exception with Simple XML framework deserialization
本文介绍了使用简单XML框架反序列化的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用简单XML序列化框架(simpleframework.org)成功序列化的XML文件进行反序列化时遇到问题)。
I'm facing a problem to deserialize an XML file which have successfully been serialized with the Simple XML Serialization framework (simpleframework.org).
异常:
org.simpleframework.xml.core.PersistenceException: Constructor not matched for class projet.sarelo.Note
这是电话:
Serializer serializer = new Persister();
File xmlFile = new File(path);
ContactList contactList = serializer.read(ContactList.class, xmlFile); <== Error
我的ContactList.java
My ContactList.java
@Root(strict=false, name="ContacList")
public class ContactList {
@ElementArray (name = "Contacts")
Contact [] contact;
}
My Note.java
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
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;
}
}
这是我正在尝试的XML文件反序列化。
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>nicolas.sarkozy@elysee.fr</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>
推荐答案
No-Arg构造函数
我不知道这个特定的XML框架,但是,通常你需要一个构造函数,它不需要为你希望反序列化的每个类提供参数/参数。这样的构造函数被称为no-arg,0-argument,或(正式) nullary constructor 。
否则,框架无法实例化该类。
Otherwise, the framework cannot instantiate the class.
这篇关于使用简单XML框架反序列化的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文