SD卡上 xml 的 XmlPullParser 不会忽略空格
我正在使用 XmlPullParser 从应用程序的嵌入式资源或从 SD 读取相同的 XML。问题是,当我从资源读取 xml 时,空白会被忽略,但 sdcard 上的 xml 不会被忽略。
以下是我如何在资源中为 xml 创建 PullParser (进一步拉取代码对于这两种情况都很常见)
XmlPullParser parser = context.getResources().getXml(resId);
在这里我为 sdcard 上的 xml 创建解析器
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
FileReader fileReader = new FileReader(filePath);
parser.setInput(fileReader);
I am reading the same XML using XmlPullParser either from embedded resource of the app or from SD. The problem is that when I read xml from resource whitespaces are ignored but for xml on sdcard are not ignored.
Here is how I create PullParser for xml in resource (further pulling code is common for both cases)
XmlPullParser parser = context.getResources().getXml(resId);
And here I create parser for xml on sdcard
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
FileReader fileReader = new FileReader(filePath);
parser.setInput(fileReader);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提示:.apk 中的 XML资源 在构建过程中被编译为二进制格式。这也可能会修剪掉“不必要的”空白。为了避免这种编译,您可以将 XML 资源放入
assets
文件夹中,并从那里读取 XML。context.getResources().getXml(resId);
当然将不再起作用;您的资产不会分配资源 ID。您必须使用原始文件名。编辑:
的文档getXml() 说:
Hint: The XML resources inside your .apk are compiled to a binary format during the build process. This may also trim away "unnecessary" whitespaces. To avoid this compilation you can put your XML resource into the
assets
folder and read your XML from there.context.getResources().getXml(resId);
will then of course not work anymore; your asset will not have a resource id assigned. You'll have to use the original filename.Edit:
Documentation for getXml() says:
我希望它可以帮助你::
http:// /developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#nextToken%28%29
I hope it may helps you ::
http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#nextToken%28%29