如何解析
问题描述:
它在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);
}
但就是不工作
答
描述元素包含一个CDATA节点。这意味着您尝试访问的<img>
“元素”实际上只是一段文本(而不是元素)。
您需要将文本解析为新的XML文档才能通过DOM方法访问它。
答
警告:这可能有点脏,如果xml可以包含类似于图片标签的注释,它也可能很脆弱。
对于具有cdata部分的简短xml片段使用xml解析的替代方法是使用regexp获取图像url。这里有一个例子:
String xml = "<description><![CDATA[<div class=\"images\"><img src=\"http://www.voicetv.co.th/cache/images/8a1a6f2aeb7b0e9c1d6bb3eae314165f.jpg\"/></div>]]></description>";
Matcher matcher = Pattern.compile("<img src=\"([^\"]+)").matcher(xml);
while (matcher.find()) {
System.out.println("img url: " + matcher.group(1));
}
+0
谢谢你可以选择。让我检查一下。 – Arslan 2011-06-10 06:48:31
我只是得到描述节点的值。放在标记。由于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