1

Bit confused about XML in ActionScript here:

I have an XML variable declared as:

public static var thisXML:XML;

and I am initialising my XML with a function that contains the following:

thisXML.white.john = false;
thisXML.black.john = false;
thisXML.white.bill = false;
thisXML.black.bill = false;
thisXML.white.pete = false;
thisXML.black.pete = false;

I then have the following String variables:

public static var blackWhite:String;
public static var thisName:String;

I want to access my XML in an 'if' statement something like this (I know this is not right):

if (!thisXML.(blackWhite).(thisName)) {
    //do something
    thisXML.(blackWhite).(thisName) = true;
}

So, I have tried many methods including:

thisXML.[blackWhite].[thisName];
thisXML.node[blackWhite].node[thisName];
thisXML.descendant[blackWhite].descendant[thisName];

... but I know this is not right. I also realise that XML does not support Boolean so I may need to use strings "true" and "false", but that is not my issue here. The issue is accessing the specific nodes using variables.

How should this be done?

Thanks for reading.

Edit: - My XML now looks like this....

 <root>
     <Documents documentName = {thisDocument}>
         <White>
             <John>false</John>
             <Pete>false</Pete>
             <James>false</James>
         </White>
         <Black>
             <John>false</John>
             <Pete>false</Pete>
             <James>false</James>
         </Black>
      </Documents>
 </root>

1 Answer 1

3

The example of access to the xml childs through the variable name:

        var xml:XML = 
            <root>
                <white>
                    <john>john white</john>
                    <bill>bill white</bill>
                    <pete>pete white</pete>
                </white>
                <black>
                    <john>john black</john>
                    <bill>bill black</bill>
                    <pete>pete black</pete>
                </black>
            </root>

        var colorName:String = "white";
        var menName:String = "bill";

        //example using []
        var nodeTextVar:String = xml[colorName][menName][0];
        trace(nodeTextVar);

        //example using XMLList.child(name)
        nodeTextVar = xml.child(colorName).child(menName)[0];
        trace(nodeTextVar);

output:

bill white
bill white
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but I'm still confused. You have created a new example which I'm not sure can be translated to the example I gave. Where you have 'nodes' I have 'black' and 'white'. There are therefore two variables that need to be used in the string where you have just used one (nodeName). It would be much clearer if you could use my example rather than creating a new one of your own. Thanks.
Ok, I updated the example using two variable node names similar (or the same, I don't sure) to your example.
I'm not sure if I should ask a new question for this but I added another node at the top of my XML called Documents with the attribute documentName. I tried to access it with thisXML.Documents.(@documentName == [docName]).child(colourName).child(menName)[0]; where 'docName' is also a variable. I also tried using the word attribute() instead of @ but whatever I did I got 'undefined'. Any idea what I'm doing wrong?
try (@documentName == docName) without brackets
Thanks, but no... that doesn't do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.