1

Can anybody show me some easy to understand guides on how to modify and update an external XML file from within ActionScript 3. I've been looking into it for a long time now, but haven't found a tutorial that I was able to understand. My goal is to create a really basic database.. Let's say, a Database of Contacts.. I want to load those contacts through an xml file. but I also want to be able to add new contacts and modify the existing ones. How do I do that?

I want to that in an Desktop AIR Application.

1 Answer 1

2

An easy task:

First, load the external XML. For the sake of an example let's imagine the XML to be of this structure:

<contactData>
    <contact firstName="John" lastName="Smith" phone="285-493-5421-793" email="[email protected]"/>
    <contact firstName="Jane" lastName="Roberts" phone="285-493-5421-214" email="[email protected]"/>
</contactData>

Second, parse that XML. For that create a value object type of a class, let's call it ContactData. It might look something like this:

package
{
    public class ContactData
    {
        public var firstName:String;
        public var lastName:String;
        public var email:String;
        public var phone:String;

        public var id:int; // always nice to store an ID

    }
}

Loop through your XML - for every contact node create a ContactData class object and fill it the data from the XML. Store an array with your ContactData objects somewhere, you'll need them later.

Third, edit the ContactData object, or even remove it from an array if you will. Adding is not a problem too.

Fourth and last, create a new xml with AS3 and loop through the ContactData object array to add contact nodes, then save the XML. Use File and FileStream classes to save the file on the hard drive or URLLoader to pass it to the server.

This is how a primitive XML creation code could look like:

var xml:XML = <contactData></contactData>;

for (var i:int = 0; i < contactDataArray.length; i++) 
{
    var cd:ContactData = contactDataArray[i];

    xml.appendChild(<contact></contact>);
    xml.contact[i].@firstName = cd.firstName;
    xml.contact[i].@lastName = cd.lastName;
    xml.contact[i].@phone = cd.phone;
    xml.contact[i].@email = cd.email;
}

I hope it's helpful and easy to understand. Good luck!

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

4 Comments

You can parse the XML into an XML instance and alter that instance directly, by adding properties, then get another string out of the edited XML object.
@Vesper, I know, but I prefer this method over direct XML editing - in my opinion it will maintain scalability of a program. Also in case if you want to show the data on the screen it's easier to use my approach and let your IDE to show what variables are available. As well as having that extra power to have functions to format input (e.g setters/getter) etc. In short - OOP is better imo.
Explicitly declared classes/value objects will perform better on a larger scale than on the fly editing of dynamic classes like the XML class. As a side note, the above XML node could be collapsed on a single line: xml.appendChild(<contact firstName={cd.firstName} lastName={cd.lastName} email={cd.email} phone={cd.phone} />); (also in the xample phone and email are swapped, but it's just an example ;) )
@GeorgeProfenza, didn't notice my typo there, thanks!

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.