本文介绍了内置一颗类" TimetableData"使用simpleframework,节省了细如XML,但是当我尝试读它在code,它引发的PersistenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  20 04-21:09:17.590:W / System.err的(23059):org.simpleframework.xml.core.PersistenceException:构造不类com.example.simplexml匹配.DayData

我在哪里,我在这似乎是做工精细的XML文件保存数据主类

 串行串行=新的持留();    TimetableData timetableData =新TimetableData();    SubjectData编程=新SubjectData(编程,心电12,1200,1400);
    SubjectData电子元件=新SubjectData(电子,心电13,1400,1600);    timetableData.daysData.get(0).subjectsData.add(电子产品);
    timetableData.daysData.get(1).subjectsData.add(编程);    文件的结果=新的文件(this.getFilesDir()的getPath()的toString()+的example.xml。);
    尝试{
        serializer.write(timetableData,结果);
    }

在这一刻,如果我看XMLFILE,我可以看到它,它看起来正常的..但是当我看到它,它抛出异常。这是我正在读它。

  TimetableData timetableData;
    尝试{
        timetableData = serializr.read(TimetableData.class,源); //这引发异常        DayData myDay = timetableData.daysData.get(0);
        SubjectData mySub = myDay.subjectsData.get(0);
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
        Toast.makeText(这一点,e.toString(),Toast.LENGTH_LONG).show();
    }

类daydata

 公共类DayData {@Attribute(NAME =DAYNAME)
私人字符串DAYNAME;
@ElementList(NAME =subjectsData,内联= TRUE)
公开名单< SubjectData> subjectsData =新的ArrayList< SubjectData>();公共DayData(字符串DAYNAME)
{
    this.dayName = DAYNAME;
}公共字符串getDayName()
{
    返回DAYNAME;
}
}

我的类TimetableData,使dayData列表

  @root
    公共类TimetableData {@ElementList(NAME =daysData,内联= TRUE)
公开名单< D​​ayData> daysData;公共TimetableData()
{
    daysData =新的ArrayList< D​​ayData>();
    daysData.add(新DayData(星期一));
    daysData.add(新DayData(星期二));
    daysData.add(新DayData(星期三));
    daysData.add(新DayData(星期四));
    daysData.add(新DayData(星期五));
}
}

我的课subjectData

 公共类SubjectData {   @Element(NAME =classLoc)
   私人字符串classLoc;   @Element(NAME =startTime时)
   私人诠释的startTime;   @Element(NAME =结束时间)
   私人诠释结束时间;   @Attribute(NAME =主旨名称)
   私人字符串主旨名称;   公共SubjectData(){
      超();
   }   公共SubjectData(字符串主旨名称,字符串classLoc,诠释的startTime,诠释结束时间){
  this.classLoc = classLoc;
  this.subjectName =主旨名称;
  this.startTime =的startTime;
  this.endTime =结束时间;
   }   公共字符串getClassLoc(){
      返回classLoc;
   }   公众诠释getStartTime()
   {
   返回的startTime;
   }   公众诠释getEndTime()
   {
       返回结束时间;
   }
   公共字符串getDayName(){
      返回主旨名称;
       }
    }

这是创建的XMLFILE

 < timetableData>
   < dayData DAYNAME =星期一>
      < subjectData主旨名称=电子>
         < classLoc>心电图13 LT; / classLoc>
         <&的startTime GT; 1400℃; /&的startTime GT;
         <结束时间> 1600 LT; /结束时间>
      < / subjectData>
   < / dayData>
   < dayData DAYNAME =星期二>
      < subjectData主旨名称=编程>
         < classLoc>心电图12 LT; / classLoc>
         <&的startTime GT; 1200℃; /&的startTime GT;
         <结束时间> 1400℃;时间/结束>
      < / subjectData>
   < / dayData>
   < dayData DAYNAME =星期三/>
   < dayData DAYNAME =星期四/>
   < dayData DAYNAME =星期五/>
< / timetableData>


解决方案

试试这个:

 公共类DayData {@Attribute(NAME =DAYNAME)
私人字符串DAYNAME;
@ElementList(NAME =subjectsData,内联= TRUE)
公开名单< SubjectData> subjectsData =新的ArrayList< SubjectData>();公共DayData(@Attribute(NAME =DAYNAME)字符串DAYNAME)
{
    this.dayName = DAYNAME;
}公共字符串getDayName()
{
    返回DAYNAME;
}
}
04-21 20:09:17.590: W/System.err(23059): org.simpleframework.xml.core.PersistenceException: Constructor not matched for class com.example.simplexml.DayData

my main class where I am saving the data in the xml File which seems to work fine

Serializer serializer = new Persister();

    TimetableData timetableData = new TimetableData();

    SubjectData programming = new SubjectData("Programming","ECG 12",1200, 1400);
    SubjectData electronics = new SubjectData("Electronics","ECG 13", 1400, 1600);

    timetableData.daysData.get(0).subjectsData.add(electronics);
    timetableData.daysData.get(1).subjectsData.add(programming);

    File result = new File(this.getFilesDir().getPath().toString() + "example.xml");
    try {
        serializer.write(timetableData, result);
    }

at this moment, if I look at the xmlfile, I can see it, and it looks alright.. but when I read it, it throws the exception. This is how I am reading it.

TimetableData timetableData;
    try {
        timetableData = serializr.read(TimetableData.class, source);//this throws the exception

        DayData myDay= timetableData.daysData.get(0);
        SubjectData mySub = myDay.subjectsData.get(0);
    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
    }

class daydata

public class DayData {

@Attribute(name="dayName")
private String dayName;
@ElementList(name="subjectsData", inline=true)
public List<SubjectData> subjectsData = new ArrayList<SubjectData>();

public DayData(String dayName)
{
    this.dayName = dayName;
}

public String getDayName()
{
    return dayName;
}
}

my Class TimetableData that makes a list of dayData

    @Root
    public class TimetableData {

@ElementList(name="daysData",inline=true)
public List<DayData> daysData;

public TimetableData()
{
    daysData = new ArrayList<DayData>();
    daysData.add(new DayData("monday"));
    daysData.add(new DayData("tuesday"));
    daysData.add(new DayData("wednesday"));
    daysData.add(new DayData("thursday"));
    daysData.add(new DayData("friday"));
}
}

my class subjectData

public class SubjectData{

   @Element(name="classLoc")
   private String classLoc;

   @Element(name="startTime")
   private int startTime;

   @Element(name="endTime")
   private int endTime;

   @Attribute(name="subjectName")
   private String subjectName;

   public SubjectData() {
      super();
   }  

   public SubjectData(String subjectName, String classLoc, int startTime, int endTime)     {
  this.classLoc = classLoc;
  this.subjectName = subjectName;
  this.startTime = startTime;
  this.endTime = endTime;
   }

   public String getClassLoc() {
      return classLoc;
   }

   public int getStartTime()
   {
   return startTime;
   }

   public int getEndTime()
   {
       return endTime;
   }
   public String getDayName() {
      return subjectName;
       }
    }

This is the xmlFile created

<timetableData>
   <dayData dayName="monday">
      <subjectData subjectName="Electronics">
         <classLoc>ECG 13</classLoc>
         <startTime>1400</startTime>
         <endTime>1600</endTime>
      </subjectData>
   </dayData>
   <dayData dayName="tuesday">
      <subjectData subjectName="Programming">
         <classLoc>ECG 12</classLoc>
         <startTime>1200</startTime>
         <endTime>1400</endTime>
      </subjectData>
   </dayData>
   <dayData dayName="wednesday"/>
   <dayData dayName="thursday"/>
   <dayData dayName="friday"/>
</timetableData>
解决方案

Try this:

public class DayData {

@Attribute(name="dayName")
private String dayName;
@ElementList(name="subjectsData", inline=true)
public List<SubjectData> subjectsData = new ArrayList<SubjectData>();

public DayData(@Attribute(name="dayName") String dayName)
{
    this.dayName = dayName;
}

public String getDayName()
{
    return dayName;
}
}

这篇关于内置一颗类&QUOT; TimetableData&QUOT;使用simpleframework,节省了细如XML,但是当我尝试读它在code,它引发的PersistenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:27