Aliaspooryorik
ColdFusion ORM Book

Consuming XML in Flex

I've been taking my first baby steps in the word of Flex and wanted to try building a Flex front end to a quiz that consumed a webservice which returns XML. The XML looks something like this:


<questions>
<question id="329D4A7E-F185-4908-8740-357EFAC536BE">
<caption>What is the capital of Brazil?</caption>
<choices>a) Rio b) São Paulo c) Brasília</choices>
<answer>c</answer>
</question>
<question id="B2AC3B1F-F110-4CE1-B642-FECA2FCBA84C">
<caption>What is the capital of Australia?</caption>
<choices>a) Canberra b) Sydney c) Erinsborough</choices>
<answer>a</answer>
</question>
</questions>

This is my mxml


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="requestXML()">

<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;

private var questionXML:XMLList;
private var qIndex:int;

public function requestXML():void
{
// cancel any running requests.
xmlWebservice.cancel();

// set up parameters.
xmlWebservice.addEventListener( ResultEvent.RESULT, handleXML ); // on successful load
xmlWebservice.addEventListener( FaultEvent.FAULT, handleFault ); // on error
xmlWebservice.showBusyCursor = true;
xmlWebservice.useProxy = false;
xmlWebservice.url = "http://www.myserver.com/webservice/quiz.cfc?wsdl";
xmlWebservice.resultFormat = "e4x";

var params:Object = new Object();

// populate the url arguments.
params = { method:"getQuestions", quiz:"{0D9177AA-94BC-4155-9704-D9284433D828}" };
params.returnformat = "plain";

// Use GET
xmlWebservice.method = URLRequestMethod.GET;
xmlWebservice.send( params );
}

public function handleXML(event:ResultEvent):void
{
// get all the question nodes and save as an array

/*
We have two ways to get the children of the questions node
1) questionXML = event.result.question;
2) questionXML = XML( event.result ).children();
*/

questionXML = event.result.question;
qIndex = 0;

DebugXML.text = XML( event.result );
DebugBox.text = "Question Count: " + questionXML.length();

btnLoadNextQuestion.visible = true;
loadNextQuestion();
}
public function checkAnswer():void
{
if ( Answer.text == '' )
{
Alert.show( "Please enter your answer!", "Error" );
}
else {
questionXML[ qIndex ].useranswer = Answer.text;
Answer.text = "";
loadNextQuestion();
}
}
public function loadNextQuestion():void
{
lblCaption.text = questionXML[ qIndex ].caption + "\n" + questionXML[ qIndex ].choices;
DebugBox.text = questionXML[ qIndex ];
qIndex++;
}
public function handleFault(event:FaultEvent):void
{
Alert.show( event.fault.faultString, "Error" );
}
]]>

</mx:Script>

<mx:HTTPService id="xmlWebservice" />
<mx:Label id="lblCaption" x="190" y="22" text="Label" width="207" fontWeight="bold" fontSize="12" height="54"/>
<mx:TextInput id="Answer" width="52" height="24" x="190" y="84" />
<mx:Button id="btnLoadNextQuestion" x="250" y="84" label="Load Next Question" click="checkAnswer();" width="147" height="22" />
<mx:TextArea id="DebugXML" width="500" height="185" x="150" y="142" />
<mx:TextArea id="DebugBox" width="500" height="182" x="150" y="356" />
</mx:Application>

As I say, I'm only just starting out with Flex but hopefully this will be useful to someone else!

 


2 comments

  1. Why not use mx.rpc.soap.mxml.WebService and then on the string returned new XML(string) ?

    Comment by radekg – February 17, 2009
  2. Hi radekg, I found that using HTTPService was more flexible as I could use it to call a hosted XML file or one generated by a script that wasn't exposed as a webservice. As I'm not dealing with complex data then I couldn't see any advantage. I'd be interested to know if there are any for this example.

    Comment by John Whish – February 18, 2009

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts