如何解析< IMG SRC ="值QUOT;从这个XML使用DOM? [英] How to parse <img src="value" from this XML by using DOM?

查看:555
本文介绍了如何解析< IMG SRC ="值QUOT;从这个XML使用DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像这样的XML。我希望得到他的图像SRC值...

It look like this in XML. I want to get he Image src value...

<description><![CDATA[<div class="images"><img src="http://www.voicetv.co.th/cache/images/8a1a6f2aeb7b0e9c1d6bb3eae314165f.jpg" /></div>]]></description>

我在做什么是

What I am doing is

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节点。这意味着&LT; IMG&GT; 元素您尝试访问实际上只是一段文字(而不是一个元素的话)

The description element contains a CDATA node. This means that the <img> "element" you are trying to access is really just a piece of text (and not an element at all).

您需要分析文本,作为一个新的XML文档,以便通过DOM方法来访问它。

You'll need to parse the text as a new XML document in order to access it via DOM methods.

这篇关于如何解析&LT; IMG SRC =&QUOT;值QUOT;从这个XML使用DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆