Featured post
java - Using "Simple" XML Serialization to load data from res/raw on Android -
i new java , android development, please keep in mind. goal deserialize data xml files packaged application. i'm attempting using simple 2.4 "unhandled exception type exception" error in code when using .read or .write
my code looks 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 activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //load in 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 class looks 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 put try / catch around it, of course error goes away, deserilization never occurs well. there little discussion on simple , documentation makes simple c# serialization. appreciated on problem.
a global view of i'm wanting have xml file each "shipsystem" , @ boot of application deserialize each 1 it's own class instance , have of these available in array lookup.
thanks taking time read , think this.
i've changed myclickhandler follows validate 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 class appear 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 xml i'm attempting deserialize looks this:
<?xml version="1.0" encoding="utf-8"?> <system id="0"> <destroyed>false</destroyed> <systemvalue>0</systemvalue> </system>
it seems method used serializer causes unhandled exception of type exception because use of serializer.validate causes same problem. missing required simplexml?
- Get link
- X
- Other Apps
Comments
Post a Comment