regular expression search with xml

故事扮演 提交于 2020-01-06 08:44:08

问题


I'm working on parsing an xml feed and ii have the following tag

<content:encoded>
<![CDATA[
<p><a href="http://www.highlandradio.com/wp-content/uploads/2013/07/An-Grianan-Hotel.jpg"><img class="aligncenter size-full wp-image-54286" alt="An Grianan Hotel" src="http://www.highlandradio.com/wp-content/uploads/2013/07/An-Grianan-Hotel.jpg" width="488" height="244" /></a></p> <p>Gardai are appealing for information about last an armed robbery at An Grianan Hotel in Burt in the earkly hours of this morning..</p> <p>Three men entered the premises at quarter to three, at least one was armed with a shotgun. They escaped with a sum of money.</p> <p>Local Superintendent Andy Archbold is leading the investigation, and has been outlining what happened&#8230;&#8230;&#8230;&#8230;..</p> <p><strong> </strong></p>
]]>
</content:encoded>

I need to be able to grab the image from this. I was wondering what is the best way to do this? could I search for a regular expression? Also with regard to the &#8230 etc. is there a way to easily convert these? All help appreciated. Below is my current code for getting the feed and looping through it, my goal is t set the leftImage element of my data array to be the image inside the content encoded tag.

var url="http://www.highlandradio.com/feed/"; 
//rss feed url
var xhr = Titanium.Network.createHTTPClient();

xhr.onload = function() {
    // Data is returned from the blog, start parsing
    var doc = this.responseXML.documentElement;

    // begin looping through blog posts
    var items = doc.getElementsByTagName("item");
    console.log(items);
    for (var i=0;i<items.length;i++) {
        data.push({                
            title: "'"+items.item(i).getElementsByTagName("title").item(0).text+"'",
            leftImage:'NewsStory.png',
            dataToPass: "'"+items.item(i).getElementsByTagName("description").item(0).text+"'",    
            className: "TableRow", 
            hasChild: true, 
            jsTest: true,
            js:"external.js"
        });
}
};

.


回答1:


Regular expression is best way for pattern search. a regex like this maybe can help you :

<img\s.*?src=\"(.*?)\".*?/>

This regex extract the scr attribute value, in your example : http://www.highlandradio.com/wp-content/uploads/2013/07/An-Grianan-Hotel.jpg



来源:https://stackoverflow.com/questions/17616087/regular-expression-search-with-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!