Popa Mihai

PopaMihai.com – Flex, Flash, AIR and ActionScript articles and tutorials

Archive for the ‘XML’ tag

ArrayCollection of One Element as DataProvider

2 comments

Flex ArrayCollections are great when using them as data providers for datagrids, lists etc.

I used to load xml files with HTTPService and store the result event in an ArrayCollection that I then used as a data provider for my components.

Here’s a typical example:

< ?xml version="1.0" encoding="utf-8"?>
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle"
	backgroundColor="white" creationComplete="init()" viewSourceURL="srcview/index.html">
 
	<mx:script>
		< ![CDATA[
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;
 
			[Bindable]
			private var dataProv : ArrayCollection = new ArrayCollection();
 
			private function init() : void {
				calendarService.send();
			}
 
			private function calendarServiceResult( event : ResultEvent ) : void {
				dataProv = event.result.events.event;
			}
 
		]]>
	</mx:script>
 
	<mx:datagrid id="dg" dataProvider="{dataProv}"></mx:datagrid>
 
	<mx:httpservice id="calendarService"
		url="http://www.popamihai.com/flex-files/ArrayCollectionOfOneElementAsDataProvider/calendar.xml"
		result="calendarServiceResult( event )"></mx:httpservice>
 
</mx:application>

The xml file calendar.xml has the following structure:

<events>
	<event>
		<date>13-07-2010</date>
		<starthour>01</starthour>
		<category>Aniversary</category>
	</event>
	<event>
		<date>12-07-2010</date>
		<starthour>02</starthour>
		<category>Meeting</category>
	</event>
	<event>
		<date>21-07-2010</date>
		<starthour>03</starthour>
		<category>To Do</category>
	</event>
</events>

As long as the xml files has more then one element everything works fine. Here’s the resulting SWF for this application:


Read the rest of this entry »

Written by Popa Mihai

August 25th, 2010 at 4:06 pm

Posted in Flex

Tagged with , , , , ,

Working with E4X – XML in Flex – Part 4 – Comments and Special Characters

leave a comment

You can add comments in your XML documents. As long as the comment in not interfering with the structure of the XML, you can add it everywhere in the XML. This means that after the comment is entered in the XML the document will still be well formatted.

<!-- This is how a XML comment looks -->

Read the rest of this entry »

Written by Popa Mihai

August 13th, 2009 at 4:21 pm

Posted in Flex

Tagged with , ,

Working with E4X – XML in Flex – Part 3 – Well Formed XML

leave a comment

A well-formed XML document is a document that follows all the XML construction rules:

  • the XML document must contain at least one element – the root element.
  • the XML document must contain a unique opening and closing tag that will contain the whole document
  • tags must be nested properly: there must be an opening and a closing tag, and the tags can not overlap
  • tags in XML are case sensitive
  • attribute values are always quoted
  • you cannot use illegal characters

Written by Popa Mihai

August 13th, 2009 at 3:58 pm

Posted in Flex

Tagged with ,

Working with E4X – XML in Flex – Part 2 – Loading XML with URLLoader

leave a comment

Another way that you can use to load xml data in Flex is by using the URLLoader class.

To use the URLLoader class you must:

  • define a new URLLoader:
    var loader:URLLoader = new URLLoader();
  • add an event listener to the newly created URLLoader:
    loader.addEventListener(Event.COMPLETE, xmlLoaded);
  • call the load method of the newly created URLLoader:
    loader.load(new URLRequest(url));
  • The load() method of the URLLoader class takes a URLRequest parameter.
    In this example I created a new URLRequest and in the URLRequest’s constructor I used the url parameter. instead of url I could used directly “xmlFile.xml” and I would have get the same result.

  • define the xmlLoaded function that will parse the loaded data

Read the rest of this entry »

Written by Popa Mihai

August 13th, 2009 at 12:37 pm

Posted in Flex

Tagged with , ,

Working with E4X – XML in Flex – Part 1 – Loading XML with HTTPService

one comment

One of the most popular and easy to use methods of loading XML data in Flex is by using the HTTPService class.

Here is how you declare a HTTPService tag in mxml:

	<mx:httpservice id="xmlRequestService" 
		url="xmldFile.xml"
		result="xmlRequestServiceResultHandler(event)"
		resultFormat="e4x"
		fault="xmlRequestServiceFault(event)"></mx:httpservice>

Read the rest of this entry »

Written by Popa Mihai

August 13th, 2009 at 12:18 pm

Posted in Flex

Tagged with , ,