它在XML中看起來像這樣。我想他圖片src值...如何解析<img src="value" from this XML by using DOM?
<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);
}
但就是不工作
我只是得到描述節點的值。放在標記。由於DOM允許1個根元素。在這裏它看起來像 DocumentBuilderFactory fectory = DocumentBuilderFactory.newInstance(); \t \t \t的DocumentBuilder助洗劑= fectory.newDocumentBuilder();的InputStream的inputStream =新ByteArrayInputStream進行((」 「+ currentStory.getDescription()+」 「).getBytes(」 UTF-8" )); Document document = builder.parse(inputStream); \t \t \t String imageURL = document.getElementsByTagName(「img」)。item(0).getAttributes()。getNamedItem(「src」)。getNodeValue(); \t \t \t currentStory.setImagePath(imageURL); – Arslan 2011-06-10 07:36:34