PDA

View Full Version : Using "For Each" XML Search in Flex 3.0, not working


lifescholar
12-24-2008, 04:32 PM
<mx:Script>
<![CDATA[

private var xmlLoader:URLLoader;
private var gallery_xml:XML;
private var xmlReq:URLRequest;
private var query:XML;
private var queryText:String = "";

private function init():void{

xmlReq = new URLRequest("gallery.xml");
xmlLoader = new URLLoader(xmlReq);
xmlLoader.addEventListener(Event.COMPLETE, completeHandler);;

}

private function completeHandler(e:Event):void {

gallery_xml = new XML(xmlLoader.data);

query = gallery_xml.image.(@id == "001").parent();

for each(var elements:XML in query.*)
{
if(elements.*.length() <= 1){
queryText += elements.name() + ":" + elements + "\n";
}
else{
for each(var info:XML in elements.*){
queryText += info.name() + ":" + info + "\n";
}
}
text_area.htmlText = query;
}


}

]]>
</mx:Script>

This returns all the xml elements, not just the single element I want. Any ideas?

This is the XML:

<?xml version="1.0" encoding="utf-8"?>

<gallery>
<image id="001">
<title>A picture</title>
<photo>The Photo</photo>
<photographer>
<name>Jack Wdelsh</name>
<location>Kansas City</location>
</photographer>
</image>

<image id="002">
<title>Jackal</title>
<photo>This is a photo2</photo>
<photographer>
<name>Jack Welddsh</name>
<location>Kansaddds City</location>
</photographer>
</image>
</gallery>

drkstr
12-28-2008, 04:12 AM
Your algorithm and XML syntax is hard to follow, so I'm not sure what you did wrong. If I were to guess, it's because you're referencing .parent() which points to the root node in that case. What exactly do you want returned? And why do you need to do any looping at all? There's probably an easier way but it's hard to say without knowing your needs.

To get a piece of item data, I would just reference it inline.

trace(gallery_xml.image.(@id == "001").title);

If you give more info about what you're trying to achieve, you will get a more meaningful response.


Best Regards,
~Aaron