开发者

How to parse <img src="value" from this XML by using DOM?

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 = allC开发者_运维百科hildern.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);
        }

But is is not working


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).

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


Warning: This might be a bit dirty, and it can also be fragile if the xml can contain comments that contains something that looks like image tags.

An alternative to using xml parsing for that short xml snippet that has a cdata section is to get the image url using regexp. Here's an example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜