Android SAXParser:格式不正确的文档...*do* 是格式良好的
因此,我有一个启动器活动,用户在其中选择一个文件(实际上是一个目录,包含 .xml 和 .wav)。
当他单击要打开的文件时,我启动一个新的活动,该活动可处理之前选择的文件。
- 向用户 Launcher_Activity 显示文件
- : 单击时: Other_Activity.path_dir = file.getAbsolutePath(); startActivity(Other_Activity_Intent);
- 其他_活动: file_source = new InputSource(new StringReader( path_dir + "/xml.xml" )); 解析器.parse(file_source)
ParseException:在第 1 行,第 0 列:格式不正确(无效标记)
就像 XML 文件为空...但是,我知道我的 path_dir 是正确的,并且我的 xml 文件也位于正确的
位置知道它是格式良好的,因为我之前使用过它。 在应用程序的早期版本中,.xml 和 .wav 是“应用程序内”,所以我使用
file_source = new InputSource(getResources().openRawResource(R.raw.xml));
使用原始资源是有效的。 但我试图将资源放在应用程序之外 我的 XML 文档不再被识别...
<?xml version="1.0" encoding="utf-8"?>
<text>
<lots of tag .../>
</text>
xml 中没有拼写错误...有人有线索吗?
编辑:
file_source = new InputSource(new StringReader( path_dir + "/xml.xml" ));
file_source.setEncoding("UTF-8");
parser.parse(file_source);
它似乎避免了格式错误的异常...但是应用程序在其他地方崩溃了,我必须追踪在哪里...
So, I have a launcher activity, where the user selects a file (in fact, a directory, containing a .xml and a .wav).
When he clic on the file he wants to open, I start a new activity which works with the previously selected files.
- display files to user
- Launcher_Activity :
onClick :
Other_Activity.path_dir = file.getAbsolutePath();
startActivity(Other_Activity_Intent); - Other_Activity :
file_source = new InputSource(new StringReader( path_dir + "/xml.xml" ));
parser.parse(file_source)
ParseException : At line 1, column0: not well-formed (invalid token)
it's like the XML file was empty ... but, I know that my path_dir is the right one, and my xml file is at the right place also
I also know that it is well-formed because I was using it before.
In a previous version of the app, the .xml and .wav were "in-app" so I was using
file_source = new InputSource(getResources().openRawResource(R.raw.xml));
Using raw resource was working.
But i'm trying to put the resource outside of the app
and my XML document is no longer recognized ...
<?xml version="1.0" encoding="utf-8"?>
<text>
<lots of tag .../>
</text>
Ther's not typpo in the xml ... Does anyone have a clue ?
EDIT :
file_source = new InputSource(new StringReader( path_dir + "/xml.xml" ));
file_source.setEncoding("UTF-8");
parser.parse(file_source);
It seems to avoid the bad-formed Exception ... But the app' crash somewhere else, I have to track down where ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试记录您获得的输入流。
我对InputSource了解不多,但我猜,正确读取文件存在一些问题。
尝试使用这个代替
Try logging the inputstream that you are getting.
I don't know about InputSource much, but I guess, there's some problem of reading the file properly.
Try using this instead
将 xml 文件放入
/assets
文件夹中,然后使用以下行:Put the xml file in the
/assets
folder and then use this line:res 目录中的 XML 文件被“预编译”为二进制形式。
这就是为什么您收到错误
将其移动到资产目录中,正如其他人建议的那样,您将能够按照您想要的方式读取它。提示:asset 文件夹位于“应用程序外部”,与 res 文件夹的范围完全相同。 IE。一点也不。
XML files inside res directory are "pre-compiled" into a binary form.
That is why you are getting the error
Move it into the asset directory as someone else suggested, and you will be able to read it the way you want to. Hint: the asset folder is "outside the app" to exactly the same extent that the res folder is. Ie. not at all.