Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Modifying XML files

February 16th, 2010 · No Comments · Tutorials

In earlier posts, I discussed various methods to read XML data (here, here).  In this post we’ll look at various means of modifying existing XML files.

In the first example below, the second node office location is changed from NY to CT by either of 2 means: the first method identifies the node to be edited by referencing its index and in the second (currently commented out) the node is defined by matching the last name.  Similarly, an attribute can be edited by using the @ notation as in the third and fourth examples.

var personnel:XML = <employees>
	<employee id='1187' lastName='Davis' firstName='Charles'>
		<office>CT</office>
		<dept>Marketing</dept>
	</employee>
	<employee id='3383' lastName='Jones' firstName='Kevin'>
		<office>NY</office>
		<dept>Sales</dept>
	</employee>
	<employee id='2946' lastName='Samuels' firstName='Elizabeth'>
		<office>CT</office>
		<dept>Engineering</dept>
	</employee>
</employees>
 
personnel.employee[1].office = 'CT';	
 
// personnel.employee.(@lastName == 'Jones').office = 'CT';
 
// personnel.employee[1].@id = '2000';
 
// personnel.employee.(@lastName == 'Jones').@id = '2000';
 
trace(personnel);

In the next example, a new employee is added using the appendChild() method.  Also, if for some reason you wanted to add the node before or after an existing node, you can use the insertChildBefore() or insertChildAfter().  Each of these two methods take 2 parameters – the first, specifies the existing node and the second the new node.  (A sample of insertChildBefore() is commented out.)

var personnel:XML = <employees>
	<employee id='1187' lastName='Davis' firstName='Charles'>
		<office>CT</office>
		<dept>Marketing</dept>
	</employee>
	<employee id='3383' lastName='Jones' firstName='Kevin'>
		<office>NY</office>
		<dept>Sales</dept>
	</employee>
	<employee id='2946' lastName='Samuels' firstName='Elizabeth'>
		<office>CT</office>
		<dept>Engineering</dept>
	</employee>
</employees>
 
personnel.appendChild( <employee id='4002' lastName='Suzuki' firstName='Kenji'>
		            <office>MA</office>
		            <dept>Sales</dept>
	                </employee> );
 
/*
// you can alternately put all of the new nodes to be inserted on a single line
personnel.insertChildBefore( personnel.employee[1],  <employee id='4002' lastName='Suzuki' firstName='Kenji'><office>MA</office><dept>Sales</dept></employee> );
*/
trace(personnel);

To remove a node use the delete method and identify the node’s index as in the code below.  If you prefer to identify the node to be deleted by matching an attribute, loop through the nodes until a match is found (see commented out code below).

var personnel:XML = <employees>
	<employee id='1187' lastName='Davis' firstName='Charles'>
		<office>CT</office>
		<dept>Marketing</dept>
	</employee>
	<employee id='3383' lastName='Jones' firstName='Kevin'>
		<office>NY</office>
		<dept>Sales</dept>
	</employee>
	<employee id='2946' lastName='Samuels' firstName='Elizabeth'>
		<office>CT</office>
		<dept>Engineering</dept>
	</employee>
</employees>
 
delete personnel.employee[0];
 
/*
// loop through all nodes
for( var i:uint=0; i<personnel.employee.length( ); i++ )
{
        // find node with matching attribute value
	if ( employee.personnel[i].( @lastName == 'Davis' ) )
	{
                // delete node with correct index
		delete employee.personnel[i];
	}
}
*/
trace(personnel);

In the next post, I’ll look at saving these changes to disk with fileReference (and caveat).  Hope you found this of value…

Post to Twitter

Tags: ····

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Spam Protection by WP-SpamFree