我有一个像这样的 Xml:
<root>
<item>
<content type="HTML">
<![CDATA[<html>
<head></head>
<body>something goes there</body>
</html>]]>
</content>
</item>
<item>
<content type="Text">
Something goes there
</content>
</item>
</root>
如果内容是文本类型,我可以使用node.getTextContent()获取。
问题是我想从Xml树中获取所有Html内容。所以我想要得到的结果是字符串:
"<html><head></head><body>some thing goes there</body></html>"
如何使用 Android 中的文档 (DOM) 做到这一点?
注意:因为我要编辑一些xml内容,所以我不能使用SAX。
最佳答案
参见 DOM 解析器示例:
http://androiddevelopement.blogspot.in/2011/06/android-xml-parsing-tutorial-using.html
xml 可能有不同类型的节点,请参阅可能的节点类型: http://www.w3schools.com/dom/dom_nodetype.asp
Android 中有多种方法可以通过 DOM 解析器解析 Xml,有用的方法如下:
getElementsByTagName - Return Array of Nodes by Tag Name specified.
getChildNodes - retervies the list of childs of Node
getAttributes()- retrieves attributes of a Node
getFirstChild- returns first child node of Node Object
getNodeName, getNodeType, getNodeValue- returns corresponding values from Node
关于java - Android 中使用 Dom 从 XML 树中获取 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10221601/