它在 XML 中看起来像这样。我想获取他的 Image src 值...
<description><![CDATA[<div class="images"><img src="http://www.voicetv.co.th/cache/images/8a1a6f2aeb7b0e9c1d6bb3eae314165f.jpg" /></div>]]></description>
我在做的是
if ((theElement.getElementsByTagName("description")).getLength() > 0) {
allChildern = theElement.getElementsByTagName("description").item(0).getChildNodes();
for (int index = 0; index < allChildern.getLength(); index++) {
description += allChildern.item(index).getNodeValue();
NodeList chNodes = allChildern.item(index).getChildNodes();
for (int i = 0; i < chNodes.getLength(); i++) {
String name = chNodes.item(i).getNodeName();
if(name.equals("div")) {
String clas = allChildern.item(index).getAttributes().getNamedItem("class").getNodeValue();
if(clas.equals("images")){
String nName = allChildern.item(index).getChildNodes().item(0).getNodeName();
if(nName.equals("img")) {
String nValue = allChildern.item(index).getChildNodes().item(0).getAttributes().getNamedItem("src").getNodeValue();
}
}
}
}
}
currentStory.setDescription(description);
}
但是是行不通的
最佳答案
描述元素包含一个 CDATA 节点。这意味着 <img>
您尝试访问的“元素”实际上只是一段文本(根本不是元素)。
您需要将文本解析为新的 XML 文档,以便通过 DOM 方法访问它。
关于android - 如何使用 DOM 从这个 XML 中解析 <img src ="value"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302712/