Android SAXParser:格式不正确的文档...*do* 是格式良好的

发布于 2024-12-28 17:23:07 字数 1138 浏览 4 评论 0原文

因此,我有一个启动器活动,用户在其中选择一个文件(实际上是一个目录,包含 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2025-01-04 17:23:08

尝试记录您获得的输入流。

我对InputSource了解不多,但我猜,正确读取文件存在一些问题。

尝试使用这个代替

FileInputStream fis = new FileInputStream(new File("path to file));
parser.parse(fis);

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

FileInputStream fis = new FileInputStream(new File("path to file));
parser.parse(fis);
梦在夏天 2025-01-04 17:23:08

将 xml 文件放入 /assets 文件夹中,然后使用以下行:

parser.parse(new InputSource(getAssets().open("xml.xml")));

Put the xml file in the /assets folder and then use this line:

parser.parse(new InputSource(getAssets().open("xml.xml")));
一片旧的回忆 2025-01-04 17:23:08

res 目录中的 XML 文件被“预编译”为二进制形式。

这就是为什么您收到错误

ParseException : At line 1, column0: not well-formed (invalid token)

将其移动到资产目录中,正如其他人建议的那样,您将能够按照您想要的方式读取它。提示:asset 文件夹位于“应用程序外部”,与 res 文件夹的范围完全相同。 IE。一点也不。

XML files inside res directory are "pre-compiled" into a binary form.

That is why you are getting the error

ParseException : At line 1, column0: not well-formed (invalid token)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文