简单的XML框架反序列化的例外

我正在面对一个反序列化XML文件的问题,这个文件已经用Simple XML Serialization框架(simpleframework.org)成功地序列化了。

抛出了一个exception :

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

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

我的Note.java

 public class Note { @Element(required=false) private String note; public Note(String note) { super(); this.note = note; } public String getNote() { return note; } } 

我的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文件。

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

Solutions Collecting From Web of "简单的XML框架反序列化的例外"

No-Arg构造函数

我不知道这个特定的XML框架,但是,通常你需要一个构造函数,它不需要每个你想要反序列化的类的参数/参数。 这样的构造函数被称为“无参数”,“0参数”或(正式) 无用的构造函数 。

否则,框架不能实例化类。

你不必从构造函数中删除东西。 你可以添加像这样的东西:

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

它为我工作:)

我认为使用SAXparsingXML更好,这是一个parsing的例子:

你需要像这样创build一个类parsing器:

  { public class DataXMLReader extends DefaultHandler { public DataXMLReader() { } public void startElement(String uri, String name, String qName,Attributes atts) { if (name.trim().equalsIgnoreCase("window")) { atts.getValue("type_id") // to get proprietis } else if (name.trim().equalsIgnoreCase("component")) { } } public void endElement(String uri, String name, String qName) throws SAXException { if (name.trim().equalsIgnoreCase("component")) { if(Integer.parseInt(typeid)<=6) idParent.remove(idParent.size()-1); } } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); Log.e("StartDoc","Reading XML"); } public void endDocument() throws SAXException { // TODO Auto-generated method stub myBdd.close(); Log.e("EndtDoc","End XML"); } } } 

这是一个从URL调用XML的例子:

 String url="http://vxbfdhbf.xml"; try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); URL sourceUrl = new URL(url); HttpURLConnection connection = null; connection = (HttpURLConnection)sourceUrl.openConnection(); connection.setConnectTimeout(60000); connection.setInstanceFollowRedirects(false); connection.connect(); DataXMLReader myXMLHandler = new DataXMLReader(this,"1"); xr.setContentHandler(myXMLHandler); xr.parse(new InputSource(connection.getInputStream())); connection.disconnect(); } catch (Exception e) { Log.e("saxERR",""+e.toString()); }