{"id":1790,"date":"2022-08-30T15:19:28","date_gmt":"2022-08-30T15:19:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/using-simple-xml-serialization-to-load-data-from-res-raw-on-android-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:28","modified_gmt":"2022-08-30T15:19:28","slug":"using-simple-xml-serialization-to-load-data-from-res-raw-on-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-simple-xml-serialization-to-load-data-from-res-raw-on-android-collection-of-common-programming-errors\/","title":{"rendered":"Using &ldquo;Simple&rdquo; XML Serialization to load data from res\/raw on Android-Collection of common programming errors"},"content":{"rendered":"<p>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&#8217;m attempting to do this using Simple 2.4 but I get an &#8220;Unhandled Exception type exception&#8221; error in the code when using .read or .write<\/p>\n<p>My code looks something like this:<\/p>\n<pre><code>import java.io.InputStream;\n\nimport android.app.Activity;\nimport android.os.Bundle;\nimport android.view.View;\n\nimport org.simpleframework.xml.Serializer;\nimport org.simpleframework.xml.core.Persister;\n\npublic class ftRoster extends Activity {\n    \/** Called when the activity is first created. *\/\n    @Override\n    public void onCreate(Bundle savedInstanceState)\n    {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.main);\n\n        \/\/Load in all available ship files here\n    }\n\n    public void myClickHandler(View view)\n    {\n\n     InputStream iStream = getResources().openRawResource(R.raw.ship);\n     Serializer serializer = new Persister();\n     ShipSystem newSystem = serializer.read(ShipSystem.class, iStream);\n\n }\n}\n<\/code><\/pre>\n<p>And my class looks like this:<\/p>\n<pre><code>import org.simpleframework.xml.Element;\nimport org.simpleframework.xml.Root;\n\n@Root\npublic class ShipSystem\n{\n @Element\n public String id = null;\n\n @Element\n public boolean destroyed = false;\n\n @Element\n public int systemValue = 0;\n\n}\n<\/code><\/pre>\n<p>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.<\/p>\n<p>A global view of what I&#8217;m wanting to do is have an xml file for each &#8220;shipSystem&#8221; and at the boot of application deserialize each one into it&#8217;s own class instance and have all of these available in an array for lookup.<\/p>\n<p>Thanks for taking the time to read and think about this.<\/p>\n<ol>\n<li>\n<p>Is it because you initialize id with null? Could you provide the xml?<\/p>\n<\/li>\n<li>\n<p>I&#8217;ve changed myClickHandler as follows to validate the xml first:<\/p>\n<pre><code>public void myClickHandler(View view)\n{\n    InputStream iStream = getResources().openRawResource(R.raw.system);\n    Serializer serializer = new Persister();\n\n    if(serializer.validate(ShipSystem.class, iStream))\n    {\n        ShipSystem newSystem = serializer.read(ShipSystem.class, iStream);\n    }           \n}\n<\/code><\/pre>\n<p>I&#8217;ve changed my class to appear like this:<\/p>\n<pre><code>import org.simpleframework.xml.Attribute;\nimport org.simpleframework.xml.Element;\nimport org.simpleframework.xml.Root;\n\n@Root(name=\"system\")\npublic class ShipSystem\n{\n    @Attribute\n    public String id = \"0\";\n\n    @Element\n    public boolean destroyed = false;\n\n    @Element\n    public int systemValue = 0;\n\n}\n<\/code><\/pre>\n<p>and the xml I&#8217;m attempting to deserialize looks this:<\/p>\n<pre><code>\n\n    false\n    0\n\n<\/code><\/pre>\n<p>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?<\/p>\n<\/li>\n<li>\n<p>I would try to define those fields as properties, with getters and setters. I don&#8217;t know how good SimpleXML works with public fields. It&#8217;s bad design, also.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:31:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;m attempting to do this using Simple 2.4 but I get an &#8220;Unhandled Exception type exception&#8221; error in the code when using .read or .write My code looks [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1790","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1790"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1790\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}