views:

59

answers:

3

I need to know how I can parse a variable path in Flex 3 & e4X. For example, I have two XML strings where the name of one element is the only difference.

<NameOfRoot>
    <NameOfChild1>
        <data>1</data>
    </NameOfChild1>
</NameOfRoot>

<NameOfRoot>
    <NameOfChild2>
        <data>2</data>
    </NameOfChild2>
</NameOfRoot>

Currently I am accessing variables like this:

var data1:String = NameOfRoot.*::NameOfChild1.*::data;
var data2:String = NameOfRoot.*::NameOfChild2.*::data;

I would rather make this task more abstract so that if "NameOfChild3" is introduced I do not need to update the code. For example:

var data:String = NameOfRoot.*::{variable}.*::data;

Does anyone have insights into how this can be done?

A: 

It looks like the ".*." operation will work. I wonder if this is the easiest way to handle this problem.

var data:String = NameOfRoot.*.*::data;
TERACytE
+1  A: 

Use the child property (LiveDocs example here):

var tagName:String = "NameOfChild1";
var data:String = NameOfRoot.child(tagName).data;

That's with no namespacing--not sure whether it's necessary in your case, but I assume you'd add some *::'s?

Michael Brewer-Davis
+1  A: 

this also works:

var data:String = NameOfRoot..data;

but if you have more than 1 data node you'll have to sort some stuff out.

invertedSpear