TERACytE
TERACytE

Reputation: 7853

How do I create variable paths using e4X?

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?

Upvotes: 1

Views: 474

Answers (3)

invertedSpear
invertedSpear

Reputation: 11054

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.

Upvotes: 1

Michael Brewer-Davis
Michael Brewer-Davis

Reputation: 14276

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?

Upvotes: 1

TERACytE
TERACytE

Reputation: 7853

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

var data:String = NameOfRoot.*.*::data;

Upvotes: 0

Related Questions