2012-04-19 60 views
1

使用DOM內容我有一個這樣的XML:獲取HTML從XML樹中的Android

<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中使用Document(DOM)?
注意:因爲我必須編輯一些XML內容,所以我不能使用SAX。

回答

-1

見DOM解析器實例:

http://androiddevelopement.blogspot.in/2011/06/android-xml-parsing-tutorial-using.html

甲XML可以具有不同類型的節點,見可能節點類型: http://www.w3schools.com/dom/dom_nodetype.asp

你在機器人的各種方法通過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 
+0

謝謝。我已經閱讀了這些API。但解決我的問題並不容易,因爲我不僅需要節點名稱和值,還需要'<,>'標籤。所以,問題是如何? – R4j 2012-04-19 05:16:42