使用简单XML框架反序列化的异常

时间:2011-09-19 12:33:48

标签: java android xml-serialization

我在使用Simple XML Serialization框架(simpleframework.org)成功序列化的XML文件进行反序列化时遇到了问题。

抛出an 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;     
}   

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

这是我正在尝试反序列化的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>

3 个答案:

答案 0 :(得分:41)

No-Arg构造函数

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

否则,框架无法实例化该类。

答案 1 :(得分:12)

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

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

这对我有用:)

答案 2 :(得分:-2)

我认为最好使用SAX来解析XML,这是解析的一个例子:

您需要创建一个类解析器:

 { 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());
        }