简体   繁体   中英

Parsing XML for attributes inside <!CData[----] ]in java DOM in android

I have an xml file like this:

<content:encoded>
  <![CDATA[ 
   <p>Some Data</p>
    <p>
     <img class="aligncenter size-large wp-image-4525" src="https://www.website.com/image1.jpg" alt="cover2" width="810" height="422" srcset="https://www.website.com/image2.jpg"/>
    Some Data
    </p>
           ]]> //end of CData
</content:encoded>

I have this android code:

private void ProcessXml(Document data) {
        if(data!=null){
            ArrayList<FeedItem>feedItems=new ArrayList<>();
            Element root=data.getDocumentElement();
            Node channel=root.getChildNodes().item(1);
            NodeList items=channel.getChildNodes();
            for(int i=0;i<items.getLength();i++)
            {
                Node currentchild=items.item(i);
                if(currentchild.getNodeName().equalsIgnoreCase("item")){
                    FeedItem item=new FeedItem();
                    NodeList itemchilds=currentchild.getChildNodes();
                    for(int j=0;j<itemchilds.getLength();j++)
                    {
                        Node current=itemchilds.item(j);
                        if(current.getNodeName().equalsIgnoreCase("title")){
                            item.setTitle(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("description")) {
                        item.setDescription(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("pubDate")) {
                            item.setPubDate(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("link")) {
                            item.setLink(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("content:encoded")) {





    //What code should I write here to get the image source/link of image.


                            }
                        }
                    }
                    feedItems.add(item);
                    }
                }
            }
        }

I want to get the 2nd link inside the xml file .The remaining code works properly, How do I access the CData and extract out only the link for the image in Java?

The CDATA tag tells the XML parser to treat the content as plain text. So if you don't want to treat it as text, but rather as XML, then you will have to extract the content as text and then submit it to another round of XML parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM