1

因此,我正在尝试从 android内部私有存储中加载预存储的 XML 文件并将其解析为 JDOM 文档。XML 与以下内容相同;

<?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>

并使用以下代码存储;

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();

本例中的解析工作正常。所以我知道 XML 是有效的。我在这里所做的只是将上述 XML 保存到私有存储中。但是,稍后,当我尝试使用以下命令从内部存储中读取该文件时;

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();

我从中得到以下日志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

有些人建议定义一个字符集可以解决这个问题,但我不能这样做,因为必须使用openFileInput(). 否则,将无法访问安全存储在内部存储中的文件。

我确定我在这里误解了一些东西。如果有人使用 JDOM 成功访问并解析了内部存储文件,我可以使用他们的输入(不是双关语)。

4

1 回答 1

2

您应该编写一个 TEXT xml 文件,而不是将对象数据序列化到文件中。

尝试新的 XMLOutputter().output(document, fos); 将修改后的文档对象序列化到存储。并且不要忘记正确冲洗和关闭 fos。

于 2014-02-28T14:48:02.063 回答