使用xml进行正则表达式搜索

时间:2013-07-12 13:29:56

标签: javascript xml regex escaping titanium

我正在解析xml Feed,并且我有以下标记

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

我需要能够从中获取图像。我想知道最好的方法是什么?我可以搜索正则表达式吗?另外关于&#8230等有没有办法轻松转换这些? 所有帮助赞赏。 下面是我获取feed并循环遍历的当前代码,我的目标是将我的数据数组的leftImage元素设置为内容编码标记内的图像。

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 个答案:

答案 0 :(得分:0)

正则表达式是模式搜索的最佳方式。像这样的正则表达式可以帮到你:

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

在您的示例中,此正则表达式提取scr属性值: http://www.highlandradio.com/wp-content/uploads/2013/07/An-Grianan-Hotel.jpg