Android XML - 在拉解析期间保存文本块

发布于 2024-09-13 20:43:46 字数 927 浏览 8 评论 0原文

我正在使用拉解析器 (org.xmlpull.v1.XmlPullParser) 解析大型 XML 文档。当我到达特定节点时,我想将它及其所有子节点作为文本块(或者只是子节点也可以)并保存为字符串。实现这一目标的有效方法是什么?有什么比(本质上)buffer.append('<').append(xpp.getName()).append('>')更好的吗?

这是一个

<root id="root">
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
</root>

例子如果您认为我最好使用其他解析器或技术,我欢迎提出建议。正如旁注 - 这些文本块将被序列化到数据库,前提是在某些时候这些文本块将被提取和解析

I'm parsing large XML document using pull parser (org.xmlpull.v1.XmlPullParser). When I reach specific node I want to grab it and all its children as chunk of text (or just children is OK too) and save as String. What would be efficient way of achieving this? Anything better than (in essence) buffer.append('<').append(xpp.getName()).append('>')?

Here's an example

<root id="root">
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
    <node>
      <grab-all-inside>
          <!-- bunch of nodes, attributes etc. that needs to be saved as text -->
      </grab-all-inside>
    </node>
</root>

P.S. If you think I'm better off using some other parser or technique I'm open for suggestions. Just as a side note - these text chunks will be serialized to db with premise that at some point these will be extracted and parsed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱*していゐ 2024-09-20 20:43:46

我怀疑您的 append() 调用已经达到了最佳效果。这些都是拉解析器中的各个事件。

如果这是 XML 资源,请考虑改用字符串资源。我知道对于 Android 1.6+,它们可以包含轻型 HTML 标记,并且它们可能可以包含任意标记,只是将其视为字符串 - 还没有尝试过。或者,为每个节点的内容创建原始资源并将整个内容作为字符串读取。

I suspect your append() calls are as good as it gets. Those are each individual events in a pull parser.

If this is an XML resource, consider using string resources instead. I know that for Android 1.6+ they can contain light HTML markup, and it may be they can contain arbitrary markup, just treated as a string -- haven't tried it. Or, create raw resources for each node's contents and read the whole thing in as a string.

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