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", requ开发者_如何学编程ired=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>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 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.
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 :)
I think it is better to use SAX for parsing an XML this is an example of parsing:
You need to create a class parser like this:
{ 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");
}
}
}
and this is an example to call XML from URL:
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());
}
精彩评论