java - Android 中使用 Dom 从 XML 树中获取 HTML 内容

标签 java android xml dom

我有一个像这样的 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/

相关文章:

java - Horizo​​ntalScrollView 与 EditText 向右跳转

java - 如何在 java 中清除控制台(使用 netbeans)

java - 有没有人在JFreeChart中做过跟随鼠标的十字准线?

java - 从具有子类的类中删除 $init$ 方法

android - Google Play 服务位置 API 有时会返回错误的位置

java - 使用从 Jackson 创建的对象在 Android 上使用 OrmLite 保存子集合

java - 发送文件到android服务器

java - 如何从生成的 JAXB 中删除命名空间

java - XML 目录可以引用单独 JAR 中的文件吗?

java - JAXB 是否提供 XMLBeanscompileXsd 的替代方案?