问题
I am new to Java and Android development, so please keep this in mind. My goal is to deserialize data from xml files packaged into my application. I'm attempting to do this using Simple 2.4 but I get an "Unhandled Exception type exception" error in the code when using .read or .write
My code looks something like this:
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class ftRoster extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Load in all available ship files here
}
public void myClickHandler(View view)
{
InputStream iStream = getResources().openRawResource(R.raw.ship);
Serializer serializer = new Persister();
ShipSystem newSystem = serializer.read(ShipSystem.class, iStream);
}
}
And my class looks like this:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
public class ShipSystem
{
@Element
public String id = null;
@Element
public boolean destroyed = false;
@Element
public int systemValue = 0;
}
If I put a try / catch around it, of course the error goes away, but then the deserilization never occurs as well. There is little discussion on Simple and the documentation makes it look as simple as C# serialization. Any help would be appreciated on the problem.
A global view of what I'm wanting to do is have an xml file for each "shipSystem" and at the boot of application deserialize each one into it's own class instance and have all of these available in an array for lookup.
Thanks for taking the time to read and think about this.
回答1:
I've changed myClickHandler as follows to validate the xml first:
public void myClickHandler(View view)
{
InputStream iStream = getResources().openRawResource(R.raw.system);
Serializer serializer = new Persister();
if(serializer.validate(ShipSystem.class, iStream))
{
ShipSystem newSystem = serializer.read(ShipSystem.class, iStream);
}
}
I've changed my class to appear like this:
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="system")
public class ShipSystem
{
@Attribute
public String id = "0";
@Element
public boolean destroyed = false;
@Element
public int systemValue = 0;
}
and the xml I'm attempting to deserialize looks this:
<?xml version="1.0" encoding="UTF-8"?>
<system id="0">
<destroyed>false</destroyed>
<systemValue>0</systemValue>
</system>
It seems that any method used with serializer causes an Unhandled exception of type exception because my use of serializer.validate also causes the same problem. Am I missing something required by SimpleXML?
回答2:
I would try to define those fields as properties, with getters and setters. I don't know how good SimpleXML works with public fields. It's bad design, also.
来源:https://stackoverflow.com/questions/4488654/using-simple-xml-serialization-to-load-data-from-res-raw-on-android