Today in France, we celebrate ""
Posts
04/07/10 : Android sorting
16/06/10 : Flex XML parsing
15/06/10 : Android settings
14/06/10 : Pax daemon
10/06/10 : Bundle CPU %
04/06/10 : Wicket CheckBox
03/06/10 : Thread monitor

By tag
Ajax
Android
AspectJ
Flex
Java
JMX
OSGi
Shell
Wickets

Here are some lights and tips on various Object Oriented Programming aspects that I would have dealt with during my software development and architecture activities..., for sharing.

"Beauty is an inexhaustible source of happyness for the one who discovers it" (Alexis Carrel)



Wednesday, 16 June 2010

Flex complex XML structure parsing

Question : in Flex, how to filter an items list embedded in a complex XML event ?

Answer :
- retrieve the exact XML path as per the corresponding embedded schema
- iterate over the received XMLList, providing this path filter


Details :

Step #1 : consider the following XML tree, containing a block of <objInst> nodes to be mapped into corresponding Java business instances :
<root>
<tag1>tag1_value</tag1>
<tag2>tag2_value</tag2>
<config>
<tag3>tag3_value</tag3>
<tag4>tag4_value</tag4>
<dir>
<objInst>
<tag1_obj>tag1_obj1_value</tag1_obj>
<tag2_obj>tag1_obj1_value</tag2_obj>
<tag3_obj>tag1_obj1_value</tag3_obj>
<objInst>
<objInst>
<tag1_obj>tag1_obj2_value</tag1_obj>
<tag2_obj>tag1_obj2_value</tag2_obj>
<tag3_obj>tag1_obj2_value</tag3_obj>
<objInst>
<objInst>
<tag1_obj>tag1_obj3_value</tag1_obj>
<tag2_obj>tag1_obj3_value</tag2_obj>
<tag3_obj>tag1_obj3_value</tag3_obj>
<objInst>
</dir>
</config>
<tag5>tag5_value</tag5>
</root>


Step #2 : upon receipt of the HTTPService ResultEvent (event below), here is the easiest/safest way to iterate over the received XMLList :
try
{
for each (var objInst:XML in new XML(event.result.config.dir).elements())
{
// accessing objInst tag1 value simply via : objInst.tag1_obj
// accessing objInst tag2 value simply via : objInst.tag2_obj
// accessing objInst tag2 value simply via : objInst.tag3_obj
// ...
}
}
catch (e:Error)
{
// In case the XML response is not properly formatted
}

Nota: the <root> XML node must be omitted in the above 'event.result.config.dir' notation, since 'event.result' already corresponds to the <root> node of the downloaded XML document.

No comments:

Post a Comment

Previous Post Next Post