SimpleXML constructor exception - cannot create inner class

I am just starting to experiment with Android Development using SimpleXML and I thought that everything was going well until I got hooked. The code below throws an exception

W / System.err (665): org.simpleframework.xml.core.ConstructorException: cannot create inner class

I looked at the questions on inner classes and I think I understand why you used them (not that mine was necessarily intentional), but despite the fact that I moved around the code to avoid using it, I'm still a little stuck and appreciate any help.

Source:

public class InCaseOfEmergencyMedAlertAllergiesActivity extends Activity { public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Serializer serializer = new Persister(); InputStream xmlstream = this.getResources().openRawResource(R.raw.sample_data_allergies); try { medalertdata allergyObject = serializer.read(medalertdata.class, xmlstream); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } setContentView(R.layout.allergies); } @Root public class medalertdata { @ElementList private List<allergy> allergyList; public List getAllergies() { return allergyList; } } @Root public class allergy{ @Element private String to; @Element private Boolean medical; @Element private String notes; public allergy(String to, Boolean medical, String notes){ this.to = to; this.medical = medical; this.notes = notes; } public String getTo() { return to; } public Boolean getMedical() { return medical; } public String getNotes() { return notes; } } 

}

With the referenced XML file:

 <?xml version="1.0" encoding="ISO-8859-1"?> <medalertdata> <allergy> <to>Penicillin</to> <medical>true</medical> <notes></notes> </allergy> <allergy> <to>Bee Stings</to> <medical>false</medical> <notes>Sample</notes> </allergy> </medalertdata> 

Is there a problem with the way I annotated SimpleXML classes or where I am trying to read them? Thank you

+7
source share
2 answers

Try removing @Root from the allergy class.

Also: do you have these two classes in a separate file: allergy.java and medalertdata.java?

+5
source

I also came across this by reading some deeply embedded XML data in Java objects (and want the structure of the objects to be simple by defining classes in a single file).

The solution (which does not include splitting into separate files) was to make the nested static classes. (In other words, convert inner classes to static nested classes .) Original in retrospect.

Example;
Nested structure:

 // ScoreData // Sport // Category // Tournament 

Java:

 @Root public class ScoreData { @ElementList(entry = "Sport", inline = true) List<Sport> sport; static class Sport { @ElementList(entry = "Category", inline = true) List<Category> category; } // ... } 

Disclaimer: I understand that the problem has already been solved, but maybe this helps others who are facing the org.simpleframework.xml.core.ConstructorException: Can not construct inner class problem and do not want to define classes in separate files as offers the answer Peter .

+12
source

All Articles