PDA

View Full Version : XML filtering help


heyday
11-25-2009, 07:56 PM
Hi AS3 coders

I'm so loving the AS3 XML filtering but have really got stuck on the following:

Here's an extract...

<screens><!--'screens' is the root node-->

<screen id="Screen2">
<text line="1" effect="null"><![CDATA[<span class="headingBold">Lorem Ipsum</span>]]></text>
<button type="ok" id="1" position="left" next="Screen8"/>
<button type="cancel" id="2" position="right" next="Screen1"/>
</screen>

<screen id="Screen3">
<text line="1" effect="null"><![CDATA[<span class="headingBold">Lorem Ipsum</span>]]></text>
<button type="ok" id="1" position="left" next="Screen8"/>
<button type="cancel" id="2" position="right" next="Screen1"/>
</screen>

</screens>


So what I want to extract is:

All button elements where position='left' in a matching screen. The matching screen is where my class variable 'currentScreen' matches the element's id (ie currentScreen='Screen3' and <screen id='Screen3'/>

Easy I thought... and in my model I wrote a function that gets it as such:

public function getButtonElementByScreenId(currentScreen:String): XMLList {

var myXmlList:XMLList = screens.screen.( attribute("id") == currentScreen ).button.(@position=="left");
}


I've tried many different combinations of the above, but now I'm stuck :-(

The following assumptions are true:

The XML is loaded
The root can be seen as 'screens'

I love this new way of getting data from XML but I'm now stuck and would really be greatful if anyone can tell me how to get the request as listed above (button elements that have an attribute position='left' and it's parent node(screen) matches my class var. I can get half of the filter working, ie, where it extracts the correct screen, but then I can't seem to extract the second filter based on what the first returns.

Many thanks

David

PS. I would love to find an intermediate to advanced tutorial that shows you some complex filters in action. If anyone knows anything online then please feel free to send me a PM. Thx

Slowburn
11-25-2009, 08:37 PM
Your code is correct, however, your function is not returning a value.

var screens:XML = <screens>
<screen id="Screen2">
<text line="1" effect="null"><![CDATA[<span class="headingBold">Lorem Ipsum</span>]]></text>
<button type="ok" id="1" position="left" next="Screen8"/>
<button type="cancel" id="2" position="right" next="Screen1"/>
</screen>
<screen id="Screen3">
<text line="1" effect="null"><![CDATA[<span class="headingBold">Lorem Ipsum</span>]]></text>
<button type="ok" id="1" position="left" next="Screen8"/>
<button type="cancel" id="2" position="right" next="Screen1"/>
</screen>
</screens>;

function getButtonByScreenId( id:String ):XMLList
{
return screens.screen.(attribute( "id" ) == id ).button.(attribute( "position" ) == "left" );
}

var but:XMLList = getButtonByScreenId( "Screen3" );
// use toXMLString() or the xml might not show up.
trace( "Button:", but.length(), but.toXMLString() );


Handy Reference. (http://www.actionscript.org/forums/showthread.php3?t=195916&highlight=E4X)

heyday
11-26-2009, 09:18 AM
Hi Slowburn

Thank you very much for your accurate answer. It worked out the box :-)

David