You should write a TEXT xml file instead of serialize the object data into the file.
Try new XMLOutputter().output(document, fos); to serialize the modified document object to storage. And do not forget to flush and close the fos correctly.
Domanda
So, I am trying to load a pre-stored XML file from the android internal private storage and parse it to a JDOM Document. The XML is identical to the below;
<?xml version="1.0" encoding="UTF-8"?>
<data_store>
<employee_table id="" fullname="" username="" password=""/>
<periodical_table id="" ref="" siteid="" siteaddress="" sitenotes="" clientid="" clientaddress="" clientnotes="" scheduled="" />
<hazard />
<compliance />
<additional siteid="" type="" value=""/>
<notification_table id="" value="" employeeid="" viewed="false"/>
</data_store>
And was stored using the below code;
SAXBuilder builder = new SAXBuilder();
doc = builder.build(ThisApplication.resources().openRawResource(R.raw.default_store));
FileOutputStream fos = ThisApplication.mApp.openFileOutput("data_store.xml", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(doc);
oos.close();
The parsing in this instance works fine. So I know the XML is valid. All I am doing here is saving said XML into the private storage. Later on however, when I try to read that file from the internal storage using the following;
SAXBuilder builder = new SAXBuilder();
FileInputStream fis = ThisApplication.mApp.openFileInput("data_store.xml");
doc = builder.build(fis); // This is line 28 in the log, where the exception is thrown
fis.close();
I get the following log from builder.build(fis);
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): null
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): org.jdom2.input.JDOMParseException: Error on line 1: At line 1, column 7: not well-formed (invalid token)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:253)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1091)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at uk.co.Foursite.datamanagement.XmlDataReader.<init>(XmlDataReader.java:28)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at uk.co.Foursite.android.AdvisorsPanelActivity.onCreate(AdvisorsPanelActivity.java:33)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.Activity.performCreate(Activity.java:5231)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.os.Handler.dispatchMessage(Handler.java:102)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.os.Looper.loop(Looper.java:136)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at java.lang.reflect.Method.invoke(Method.java:515)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at dalvik.system.NativeStart.main(Native Method)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): Caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 7: not well-formed (invalid token)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
02-28 13:07:16.212: E/AdvisorsPanelActivity(5628): ... 18 more
Some have suggested that defining a character set can fix this problem but I can't do that because the FileInputStream must be retrieved using the openFileInput()
. Otherwise, there is no way to access a file stored securely within Internal storage.
I am sure I have misunderstood something here. If anyone has successfully accessed and parsed an Internal Storage file with JDOM, I could use their input (no pun intended).
Soluzione
You should write a TEXT xml file instead of serialize the object data into the file.
Try new XMLOutputter().output(document, fos); to serialize the modified document object to storage. And do not forget to flush and close the fos correctly.