Skip to content
June 27, 2012 / Danii Oliver

How to parse XML or RSS node with a : colon

While building an RSS feed for a website update I ran into an XML node format that I had not encountered before.
I built the code in php and jQuery JavaScript to find a work around and end up with the code below that works.

in PHP you could try to ge the namespace and define it  foreach ($dom->getElementsByTagNameNS(‘http://api-url’, ‘*’) as $element) but it only worked when the xml was written in.

Basically you have to type  \\ to escape the colon or write as .find(“[nodeName=z:row]”) which is suppose to work in all modern browsers as webKit may have and issue with escaping the : colon

The XML

<item>
<title>Some Title Here</title>
<link>http://abc.com/rss</link&gt;
<media:content url=”http://abc.com/thumbStandard.jpg&#8221; medium=”image” height=”75″ width=”75″/>
<media:description>The Target Text</media:description>
<media:credit>Danii Oliver/Developer</media:credit>
<description>An and that I don’t want to show up.
&lt;a href=&quot;http://ads.pheedo.com/click.phdo?s=e7aaf7acc0d18030e69e81044118d586&amp;p=1&quot;&gt;&lt;img alt=&quot;&quot; style=&quot;border: 0;&quot; border=&quot;0&quot; src=&quot;http://ads.pheedo.com/img.phdo?s=e7aaf7acc0d18030e69e81044118d586&amp;p=1&quot;/&gt;&lt;/a&gt;
</description>
</item>

Working Code to parse XML

<script type=”text/javascript”>
$.ajax({
type: “GET”,
url: “movies.rss”,
dataType: “rss”,
success: function(xml) {
$(xml).find(‘item’).each(function(){
var title = $(this).find(‘title’).text();
var link = $(this).find(‘guid’).text();
var description = $(this).find(‘media\\:description’).text();

or

var description = $(this).find(‘[nodeName=media:description]’).text(); **Did not work for me
alert($(this).find(‘media:description’).text());
$(‘#feeds’).append(‘<div class=”items”><a href=”‘+link+'”><h2>’+title+'</h2></a><p>’+description+'</p></div>’);
});
}
});
</script>

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: