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
Contributor
kingsha
Posts: 23
Registered: ‎07-11-2011
My Device: Beginner Developer

How can i get XML file contents from external URL?

Hi there. I am new to webworks and blackberry development. I tried searching for my answer but could not find anything. Maybe someone can help me as this probably is an easy question.

 

I wish to get the contents of an xml file from a url and display the contents in a nice format. I have tried using the following  2 js functions to no avail:

function loadMyXML() {
    alert('hi...start!');
    var xhr_object = new XMLHttpRequest;
    xhr_object.open("GET", "****.ca/aa/aa/aa/maag.xml", false);
    xhr_object.send(null);
    alert('done');
}
function loadXML(url) {
    alert('hi!');
    if (document.implementation && document.implementation.createDocument) {
	var xmldoc = document.implementation.createDocument("", "", null);
	//xmldoc.onload = function() { handler(xmldoc, url); }
        xmldoc.load(url); 
alert('im done!'); } else alert('it failed!'); }

 

I call them but they just dont complete (dont get the alert messages placed at the end).

 

Can someone tell me how I can extract the XML from an external URL? Thanks in advance!

 

 

Contributor
aesculus
Posts: 22
Registered: ‎05-06-2011
My Device: torch 9800

Re: How can i get XML file contents from external URL?

[ Edited ]

Looking forward to the response. I have had a similar question posted for weeks with no answer using jquery get. It appears that off domain requests are just ignored.  Not sure if we need a different certificate, proxy or what.

Contributor
kingsha
Posts: 23
Registered: ‎07-11-2011
My Device: Beginner Developer

Re: How can i get XML file contents from external URL?

Any ideas? :Helpsmilie:

Contributor
kam1loherrera
Posts: 19
Registered: ‎06-14-2011
My Device: 9300

Re: How can i get XML file contents from external URL?

Hello, the external domain is white listed on your config.xml? (Permissions section) if not, add it (remember to check Apply access requests to subdomains).

 

And remember that XML requests are restricted by the "Same origin Policy" http://en.wikipedia.org/wiki/Same_origin_policy so if it doesn't work try JSONP (you wil need a JSONP aware web service)

 

Try using jquery library it will save you a lot of work.

Contributor
kingsha
Posts: 23
Registered: ‎07-11-2011
My Device: Beginner Developer

Re: How can i get XML file contents from external URL?

Thanks for the reply kam!

 

Yes, I have this code snippet in my config.xml:

<access subdomains="true" uri="http://****/aaa/aaa/maag.xml"/>

 

But, you got me at the 'same origin policy'. Are you sure it would apply to such a case? I thought javascript could access any page.

 

How exactly will I go about using a jquery library in the webworks framework? (sorry I am new to this). I thought webworks was only html, js, css. Please correct me if I am wrong!

Contributor
kam1loherrera
Posts: 19
Registered: ‎06-14-2011
My Device: 9300

Re: How can i get XML file contents from external URL?

Yep cross domain requests are banned, except when they are pure javascript, in this case you are retrieving xml code. And about jquery just download the script, add it as any other javascript file and there you have it. Also check the basics like connectivity to the external domain, etc. I'll post some code in a minute, hope it helps.
Contributor
kam1loherrera
Posts: 19
Registered: ‎06-14-2011
My Device: 9300

Re: How can i get XML file contents from external URL?

this is a JSONP ajax request, check jquery .ajax() documentation so you can play a litte more with the config:

 

//HTML jquery include

<script type="text/javascript" src="jquery/jquery-1.6.1.min.js"></script>


//javascript

function queryService(srvUrl, callbackFunc){
	var timeout_json = 30; //seconds
	//show page loading or wait message
	
	//$.ajax
	$.ajax({
		type: "GET",
		url: srvUrl,
		crossDomain: true,
		cache: false,
		dataType: "jsonp",
		jsonp : "callback", //Server only supports JSONP param name "callback"
		jsonpCallback: callbackFunc,
		timeout: timeout_json,
		error: (function(xhr, ajaxOptions, thrownError){
			//hide page loading or wait message
			//show error message ie. "Status Code: ("+xhr.status+")<br>Description: "+xhr.statusText
		}),
		success: (function(data){
			//hide page loading or wait message
			//data may be an object or a simple string depends on the json format returned by the service
                        //you can use your response data here and also on you callback function.
		})
	});
}

//function usage
queryService("http://www.mysite.com/myservice.php","mycallbackfunc");

function mycallbackfunc(data){
  //alert or something fancy.
}