Android:从 SAX 元素获取 XML
我可以从 Android 中的 SAX Element 类获取 XML 字符串吗?例如,在.NET中的DOM解析中,我们可以获取节点的OuterXml或InnerXml。除非我忽略了某些事情,否则似乎没有办法在 Android 中使用 SAX 或 DOM 解析来做到这一点。我的说法正确吗(我希望不是)?
使用 SAX,我们可以执行以下操作:
Element childElt = rootElt.getChild(CHILD_ELT_NAME);
if(childElt != null) {
childElt.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
//Do something with the body string
}
});
}
这将使我们能够获取子元素的“值”。有没有类似的东西可以让我们获得元素及其子元素的 XML 表示形式?
谢谢。
Can I get a string of XML from a SAX Element class in Android? For example, in DOM parsing in .NET, we can get the OuterXml or InnerXml of a node. Unless I'm overlooking something, it seems that there is no way to do that either using SAX or DOM parsing in Android. Am I right about that (I hope not)?
Using SAX, we can do something like:
Element childElt = rootElt.getChild(CHILD_ELT_NAME);
if(childElt != null) {
childElt.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
//Do something with the body string
}
});
}
That will allow us to get at the "value" of the child element. Is there anything similar that will allow us to get at the XML representation of the element and its children?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试执行对树结构敏感的操作,则应该使用 DOM。您似乎想要做的事情似乎符合该描述,因此我建议您使用 DOM 解析器。
有关 SAX 与 Dom(事件与树)的讨论,请参阅:http://www.saxproject.org /event.html
If you are attempting to do operations that are tree structure sensitive, you should use DOM. What you appear to be trying to do would seem to fit that description, so I would suggest you use a DOM parser.
For a discussion of SAX v. Dom (Event vs. Tree) see: http://www.saxproject.org/event.html
使用 VTD-XML,您可以获取令牌索引并将其转换为字符串以进行进一步处理(如果简单的话)你在寻找什么。
With VTD-XML, you can get the token index and convert them to string for further processing, if simplicity is what you look for.