coner
coner

Reputation: 270

Changing content of XML

There is an XML file and it consists of various nodes. I need to change one attribute of the nodes and then overwrite everything as a new version. I have found that SO question but Flash IDE cannot understand my XML file at all and throw errors about syntax.

I have calculations during the programme and should be written in specific attribute in XML. Then the updated version of the file replaced with former (overwrite).

My XML file are huge and complex, thus I am unable to put all my codes here. However I can tell you that I get everything from the file and put those in the code like link above.

<?xml version="1.0" encoding="UTF-8"?><!--I guess this part is not recognised and need to be added just before to save-->
<Something x="0" y="0" width="1437.5" height="1010">

Flash expects a ; after the x="0" and stops expectedly.

What is the proper way to change the attribute of an XML?


Edit:

I am using an array for the XML file by slicing into pieces. Then I add necessary parts and keep like that. But I wonder is there better way of doing it without messing with arrays.

Upvotes: 0

Views: 51

Answers (1)

akmozo
akmozo

Reputation: 9839

To change an XML attribute, you can use E4X ( ECMAScript for XML ), like this :

var xml:XML = <objects>
    <object id="mc_01" x="0" y="0" width="1437.5" height="1010" />
    <object id="mc_02" x="300" y="400" width="150" height="100" />
    <object id="mc_03" x="120" y="0" width="50" height="30" />
</objects>;

// select the object with its id (for example), and change its "x" attribute
xml.*.(@id == 'mc_02').@x = 800;

trace(xml);

// gives
//
//  <objects>
//    <object id="mc_01" x="0" y="0" width="1437.5" height="1010"/>
//    <object id="mc_02" x="800" y="400" width="150" height="100"/>
//    <object id="mc_03" x="120" y="0" width="50" height="30"/>
//  </objects>

For more about working with XML in ActionScript 3, take a look here.

Hope that can help.

Upvotes: 1

Related Questions