Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 07:21, 5 November 2007.

How to load and parse XML files

From Forum Nokia Wiki


Compatibility: Flash Lite 2.x

This application fails when trying to establish a network connection on the N95 (FL 2.0)

We will be using this basic XML document:

<myPhone>
	<modelName>Nokia N73</modelName>
	<screen>
		<height>240</height>
		<width>320</width>
	</screen>
	<cpu>
		<type>ARM 9</type>
		<clock_rate>220</clock_rate>
	</cpu>
</myPhone>


Contents

Loading XML files

This following script loads an XML file:

// create a new XML object
var myPhoneXML = new XML();
 
//Text nodes that contain only white space are discarded
myPhoneXML.ignoreWhite = true;
 
// define the onload handler
myPhoneXML.onLoad = function(success) {
	if (success) {
		trace("XML loaded");
	} else {
		trace("Loading failed");
	}
};
 
// load the XML file into the myPhoneXML object
myPhoneXML.load("myPhone.xml");
  • The load() method on the XML object allows you to download data from a remote host.
  • When the data has finished downloading, the onLoad event of the XML object occurs.

XML Navigation

  • firstChild: The first child in the parent node's child list
trace(myPhoneXML.firstChild);
// Output: <myPhone>[...]</myPhone>
 
trace(myPhoneXML.firstChild.firstChild);
// Output: <modelName>[...]</modelName>


  • lastChild: The last child in the parent node's child list
trace(myPhoneXML.firstChild.lastChild);
// Output: <cpu>[...]</cpu>


  • nextSibling: The next node in the parent node's child list
trace(myPhoneXML.firstChild.firstChild.nextSibling);
// Output: <screen>[...]</screen>


  • previousSibling: The previous node in the parent node's child list


Image:xml_nav.jpg


Getting nodes value

  • This example checks the <clock_rate> node value:
trace(myPhoneXML.firstChild.lastChild.lastChild.firstChild.nodeValue);
// Output: 220


  • Another example using user XMLNode objects to get the screen resolution
var screenNode:XMLNode = myPhoneXML.firstChild.firstChild.nextSibling;
 
trace("Resolution: " + screenNode.firstChild.firstChild.nodeValue + " x " +
screenNode.lastChild.firstChild.nodeValue);
// Output: Resolution: 240 x 320
  • Another example by iterating through the child nodes
for (var aNode:XMLNode = myPhoneXML.firstChild.firstChild.nextSibling.firstChild;aNode != null;aNode=aNode.nextSibling) {
    trace(aNode.firstChild.nodeValue);
}
// Output: 240
//	   320

Download

You can download an example with source code here:

This Flash Lite 2.x application gets and shows information about the 5 latest Nokia devices. It uses RSS feeds and XML Flash Lite methods. Latest_Nokia_Devices_240*320.zip
Image:Nokia_Latest_Devices.jpg

Related Discussions
Thread Thread Starter Forum Replies Last Post
Hide source? RICH? Python 11 2006-04-05 17:10
XML parsing problem tugmbh Mobile Java General 1 2006-06-17 15:41
请Cxt_programmer来帮个忙 isarc Symbian 2 2008-04-30 05:24
Nokia series 40 phones: cctivating provisioned settings seancronin OMA DM/DS/CP 7 2004-01-07 17:46
获取exe的路径 shsf Symbian 2 2004-07-16 02:54
 
Powered by MediaWiki