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 Contributor
Posts: 3
Registered: ‎01-11-2012
My Device: Blackberry Bold 9780
My Carrier: MTC Touch

How do I parse XML data from a remote file in my WebWorks application?

Okay, so I'm a beginner in WebWorks Application development, and I'm stuck at this:

 

For my development environment, I'm using Dreamweaver CS5.5, and my application uses the jQuery mobile framework. The data is found on a remote XML file hosted on my Apache server.

 

The XML file looks as follows:

 

<elements>

<news>

<news_item>

<id>0</id>

<title>Random Title 1</title>

<post_date>11-01-2012</post_date>

<content>Some random content</content>

</news_item>

<news_item>

<id>1</id>

<title>Random Title 2</title>

<post_date>11-02-2012</post_date>

<content>Some random content</content>

</news_item>

<news_item>

<id>2</id>

<title>Random Title 3</title>

<post_date>11-03-2012</post_date>

<content>Some random content</content>

</news_item>

</news>

<users>

<user>

<id>0</id>

<name>FirstName1 LastName1</name>

<group>1</group>

</user>

<user>

<id>1</id>

<name>FirstName2 LastName2</name>

<group>2</group>

</user>

<user>

<id>2</id>

<name>FirstName3 LastName3</name>

<group>3</group>

</user>

</users>

</elements>

 

-----------------------

 

Here's the scenario:

 

My application has 4 jQM pages.

On one of my pages I included a jQM list component. I would like to know how to populate this list with new data coming from the XML file and how to parse it to display a dialog box with its data.

 

Another thing is, I also have a jQM select menu on another page which I would also like to populate with 'users' from the XML file.

 

I tried jParse, but data would not be visible in my Blackberry's browser.

I also tried many different methods and functions. Still no luck! 

 

 

So i would really appreciate any help!

 

Thanks in advance,

Mansour.

Contributor
Posts: 47
Registered: ‎06-06-2011
My Device: bolt2
My Carrier: digicel jamaica

Re: How do I parse XML data from a remote file in my WebWorks application?

This actually very easy without jquery. I dont use jquery so i cant show you how is is done that way. I can show you how it is done using pure javascript. here goes: 

function processXML(){
	
try {
		// Assign callback function
        var request = new XMLHttpRequest();
		request.onreadystatechange = function(){
			if (request.readyState == 4){
			    if (request.status == 200){
					
				var xmlDoc = request.responseXML;
				
				//this is the importline in your xml example.
			    var dataRows = xmlDoc.getElementsByTagName("news_item");
				
			    
					
			     db.transaction(function(tx) {
				 
				
					
			    for(var i=0;i<dataRows.length;i++ ){
				 
				 //do something with the values example such insert in a local database
				 
			     tx.executeSql("INSERT INTO news(id, title, post_date,content ) VALUES (?,?,?,?)", [xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue,xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue,xmlDoc.getElementsByTagName("post_date")[i].childNodes[0].nodeValue,xmlDoc.getElementsByTagName("content")[i].childNodes[0].nodeValue], null,onError);
				 

				 
			
			     }//end for loop
				 

				 
				},onError);//end db.transaction(function(tx) 
					
				} else if (request.status == 500) {
					
					
					alert("HTTP 500 Error. Error calling local xml file");
					
				}//end if request.status == 500
				
			}
			
			
		}
		
		// URL for Web Method / API
		
		request.open("GET","http://www.somedomain.com/build_xml_news.xml", true);
		request.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
		request.setRequestHeader("Pragma", "no-cache");
		request.setRequestHeader("Cache-Control", "no-transform");
		request.send();
		
	} catch (ex) {
		errMessage = errMessage + "\n processXML : " + ex;
		alert("Failed to get data from the local folder. "+errMessage);
		return null;
	}
	
	
	
	
}

 

New Contributor
Posts: 3
Registered: ‎01-11-2012
My Device: Blackberry Bold 9780
My Carrier: MTC Touch

Re: How do I parse XML data from a remote file in my WebWorks application?

Well, I don't know how to thank you. But there is one little problem with that code, I couldn't get it to work with a remote file stored on the server. I managed to parse an XML file located in the same directory as my index page. Not a big problem though, solvable Smiley Happy Thanks alot, Mansour
Contributor
Posts: 47
Registered: ‎06-06-2011
My Device: bolt2
My Carrier: digicel jamaica

Re: How do I parse XML data from a remote file in my WebWorks application?

For this to work on a remote domain you have to whitelist the domain in the config xml file. like this: 

 

<access uri="*" subdomains="true"/> // for all doamins

<access uri="http://somedomainname.com" subdomains="true"/> // for a specific doamins

<access uri="local:///somefoldernamewithxamlfile/" subdomains="true"/> // for a local folder in the installation folder with the xml file

 

if you embed the file in a local installation folder you would have to use something like this o access the embed xml file:

 

request.open("GET","local:///somefoldernamewithxamlfile/build_xml_news.xml", true);

 

If this was of any help please indicate.

New Contributor
Posts: 3
Registered: ‎01-11-2012
My Device: Blackberry Bold 9780
My Carrier: MTC Touch

Re: How do I parse XML data from a remote file in my WebWorks application?

Aha , well i'll use that whilst finishing the code for parsing the data. As for now, I'm still debugging using my phone's browser and not actually installing the native app. I guess i should keep that in mind before I compile the final release. Thanks for all the help anyway!