I’m having a bit of problems with simple XML coding. I’m using a simple flash application to write a XML containing customer data (simple stuff, like phone number, name, email, etc).
I understand how to write XML manually, but my issue comes when I want to create a element inside another element. I’m using AS3.
So, for example, I have the following xml.
<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
</xmlcontainer>
If I want to add a new element, thats fine. But i’m not sure how to add a element INSIDE once its done in code.
I have been using .appendChild(<client/>) so far, but errors pop up as I do element inside element. I tried writing as a text element (i.e., manually) by just doing .appendChild("<client><name>Marco</name></client>"), but the less than and great than symbols don’t pass along correctly.
Can someone help me out here?
EDIT: As requested, here is the full code.
function appendXML():void{
var xmlData:XML = new XML();
var xmlrequest:URLRequest = new URLRequest(String("xml/clientelist.xml"));
xmlLoader.load(xmlrequest);
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
xmlData.appendChild(<pessoa/>);
xmlData.appendChild(<id/>);
xmlData.id.appendChild(idfield.text);
xmlData.appendChild(<nome/>);
xmlData.nome.appendChild(nomefield.text);
xmlData.appendChild(<email/>);
xmlData.email.appendChild(emailfield.text);
xmlData.appendChild(<contacto/>);
xmlData.contacto.appendChild(contacto1field.text);
trace(xmlData);
var fileb:FileReference = new FileReference;
fileb.save( xmlData, "clientelist.xml" );
}
(pessoa = person, nome = name, contacto = phonenumber)
I get no errors with your code.
I modified the text field values and hard coded as a string for testing .
So what errors are you getting?
Sounds to me like your xml response from the server might be broken.
The following code is a working example, although I don’t thinkthe data is structured the way you want.
And to sum up your question.
When you add a node “client” in your case you can only target it in 2 ways.
The first way is to create an XMLList and loop through it until you find the one you are looking for. This is due to the fact that you have multiple “clients”.
The second method would be to Id the clients somehow for example an attribute.
If you know the Id you can target that specific node easy.