如何从 XML 获取嵌入 XML
我有一个小问题,在向服务器发送请求期间,我收到了带有嵌入 xml 的 xml。应用程序在 Android 2.1 上运行,因此我不能仅使用 getTextContent()
来获取值,因此我使用 Node.getFirstChild().getNodeValue();
并用于标准对于带有文本的节点,它工作正常,但对于其中嵌入 xml 的节点,则不然。有人有任何建议或遇到同样的问题吗?怎么解决呢。
顺便说一句:当我检查节点类型时,所有这些都是 1,所以这意味着 ELEMENT_NODE。
I have a little problem, during sending request to serwer I'm getting xml with embedded xml in it. Application works on Android 2.1 so I can not just use getTextContent()
to get value, so I'm using Node.getFirstChild().getNodeValue();
and for standard node with text it works fine but for node with embedded xml in it, it isn't. Does someone have any advice or had the same problem? How to solve it.
BTW: When I checked type of node all of the are 1 so it means ELEMENT_NODE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您到底从服务器收到什么?像这样的东西;
或者;
在后一种情况下,您可以简单地检索数据节点(而不是值),无需单独解析它,而第一种情况并不完全有效。
What exactly are you receiving from server? Something like;
Or;
In latter case you can simply retrieve data -node (not value), there's no need to parse it separately, while first case isn't exactly valid afaik.
您不能在 XML 文档中嵌入任意 XML 文档,因为 XML 文档可能包含对于首先嵌入 XML 所需的 CDATA 节来说是非法的序列。
如果任务能够嵌入然后提取任意 XML 文档,则必须对 XML 文档进行编码,然后将其嵌入到 CDATA 部分中。原因是 CDATA 的结束标记是“]]>”如果(任意)嵌入文档包含 CDATA 部分,则“]]>”该文档 CDATA 部分的标记将关闭父级中的 CDATA 部分。
根据我的经验,在 XML 中嵌入 XML 的最佳方法是压缩(因为 Base64 编码会破坏它......)然后进行 Base64 编码。我采用这种方法的原因是因为它是万无一失的,并且两种转换(某种压缩器和 Base64 编码/解码)可以广泛使用。
You can't embed an arbitrary XML document in an XML document because XML documents can include sequences which are illegal with CDATA sections that are required to embed XML in the first place.
If the task is being able to embed, then extract, an arbitrary XML document, you have to encode the XML document, then embed it in a CDATA section. The reason is that the closing tag for CDATA is "]]>" and if the (arbitrary) embedded document contains a CDATA section, the "]]>" tag for that documents CDATA section will close the CDATA section in the parent.
In my experience, the best approach to embedding XML inside of XML is to compress (because Base64 encoding is going to blow it up ...) then Base64 encode. The reason I've taken this approach is because it is fool-proof and the two transforms (some kind of compressor and base64 en/de-coding) are widely available.