Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
Posts: 13
Registered: ‎12-03-2009
My Device: Not Specified
Accepted Solution

Get an XML code from a URL in the widget

We are making a Blackberry widget to display RSS feeds. So we want to get the XMl code of the RSS feeds, say from the link,

http://www.puuba.com/team/blog.rss

 

Right now, we are directyl opening the link and showing the feeds on the browser using href in html. But we want to categorize the feeds based on the location and date, so we want to get the XML tags and then parse it using an XML parser using Javascript.

 

Please let us know asap.

 

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Get an XML code from a URL in the widget

You should be able to retrieve the URL http://www.puuba.com/team/blog.rss in an AJAX request.  The responseXML property of the XMLHttpRequest object is an already parsed XML object.  You can then look at the contents of the XML and categorize it accordingly.

 

Here is an interesting article on the topic I found:

http://articles.techrepublic.com.com/5100-10878_11-6058865.html

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
New Developer
Posts: 13
Registered: ‎12-03-2009
My Device: Not Specified

Re: Get an XML code from a URL in the widget

I am trying to do that but I get errors using XMLHttpRequest(), xmlhttp.onreadystatechange saying the methods are not resolved.

 

This is the code I am using:

 

I have created a blackberry web project RSSXML and made files index.html which looks like this:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" id=”viewport” content="initial-scale=1.0,user-scalable=no">
<script type="text/javascript" src="getrss.js"></script>
<title>Insert title here</title>
</head>
<body>

<form>
Select an RSS-feed:
<select onchange="showRSS(this.value)">
<option value="Google">Google News</option>
<option value="MSNBC">MSNBC News</option>
</select>
</form>

<p><div id="rssOutput">
<b>RSS-feed will be listed here...</b></div></p>
</body>
</html>

 

and getrss.js is as follows:

 

//<script language="javascript">
var xmlhttp;

function showRSS(str)
  {
    xmlhttp=new XMLHttpRequest();
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
  var url="getrss.php";
  url=url+"?q="+str;
  url=url+"&sid="+Math.random();
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }

function stateChanged()
  {
  if (xmlhttp.readyState==4)
    {
    document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
    }
  }

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

 

 

and I made a file and named it getrss.php and put it in this project.

 

<?php
//get the q parameter from URL
$q=$_GET["q"];

//find out which feed was selected
if($q=="Google")
  {
  $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
  }
elseif($q=="MSNBC")
  {
  $xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");
  }

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++)
  {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;

  echo ("<p><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br />");
  echo ($item_desc . "</p>");
  }
?>

 

 

Firstly, I want to know why I am getting errors using the XMLHTTPRequest and any of its methods.

Secondly, am I allowed to use PHP like the way I am using in blackberry widgets?

 

Please let me know as soon as u can, m in a rush. Thanks