3
/* I start with this: */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>true</prop5> 
</Report>

/* I want this result (change the value of node "prop5"): */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>false</prop5> 
</Report>

/* I tried this: */

var reportXML:XML = 
    <Report>
        <prop1>4</prop1> 
        <prop2>2255</prop2> 
        <prop3>true</prop3> 
        <prop4>false</prop4> 
        <prop5>true</prop5> 
    </Report>;

var myArray:Array = [{xmlNodeName: "prop5", value: false}];

for each (var item:Object in myArray)
{
    report.xml[item.xmlNodeName] = item.value.toString();
}

/* But this just adds a new node, resulting in this: */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>true</prop5> 
    <prop5>false</prop5> 
</Report>;

5 Answers 5

5

This seems to be doing exactly what you want. It's just your code with some typos fixed.

        var reportXML:XML = 
            <Report>
                <prop1>4</prop1> 
                <prop2>2255</prop2> 
                <prop3>true</prop3> 
                <prop4>false</prop4> 
                <prop5>true</prop5> 
            </Report>;

        var myArray:Array = [{xmlNodeName: "prop5", value: false}];

        for each (var item:Object in myArray)
        {
            reportXML[item.xmlNodeName] = item.value.toString();
        }

        trace(reportXML);
Sign up to request clarification or add additional context in comments.

1 Comment

I'll give you credit for this one.... I've got it working now. Both in pseudo code and real code. Thanks.
2

I've not been able to edit elements within an XML object after the object has been created and the Adobe documentation isn't clear on if this is even possible.

For dynamically setting values, I created a temporary string and appended all my XML Nodes and Attributes in here. Then you can simply create the xml object specifying your temp string as the lone parameter.

Something like:

var tempString:String = "<XML_PARENT><SOME_SUB_NODE>";
tempString += "<SOMETHING_ELSE value=\"" + someTextField.text + "\"/>";
tempString += "</SOME_SUB_NODE></XML_PARENT>";

var xmlObj:XML = new XML( tempString );

Now if you trace xmlObj, you'll get

<XML_PARENT>
    <SOME_SUB_NODE>
        <SOMETHING_ELSE value=""/>
    </SOME_SUB_NODE>
<XML_PARENT>

This will let you dynamically assign whatever you want to the string and then build the XML after the fact. It's not exactly helpful if you then want to edit an existing XML object, but you could just use toString() and modify the string accordingly. It might at least help in the to get started with dynamically building XML files!

Comments

1

I just verified that this works:

private var reportXML:XML = 
    <Report>
        <prop1>4</prop1>
        <prop2>2255</prop2>
        <prop3>true</prop3>
        <prop4>false</prop4>
        <prop5>true</prop5>
    </Report>;

private function changeXML():void {
    reportXML.prop5[0] = 'false';
    trace(reportXML.prop5);  // traces 'false'
}

1 Comment

This still does not answer my original question. Look at my original example - I want this to be DYNAMIC. I have an Array of Objects that specify the nodes to be updated. My whole purpose in posting this is to find a way to not have to hardcode "prop5" into my application.
1

if you only have the node you can go like this

var node:XML
inp = new textfield(style, node.text());
inp.addEventListener(TextEvent.TEXT_INPUT, change, false, 0, true);
addChild(inp);

private function change(e:TextEvent):void
 {
 XML(node.parent())[node.name()][node.childIndex()] = inp.text+e.text;
 }

Comments

-2

Using E4X syntax in ActionScript 3 I guess that would be something like:

report.prop5[0] = false;

1 Comment

This didn't work either. This created a new node with a different namespace (the namespace was based on the name of the class I was in).

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.