Reputation: 433
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)
Upvotes: 1
Views: 5544
Reputation: 6402
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.
var xmlData:XML = new XML(<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
</xmlcontainer>
);
xmlData.appendChild(<pessoa/>);
xmlData.appendChild(<id/>);
xmlData.id.appendChild('idFieldText');
xmlData.appendChild(<nome/>);
xmlData.nome.appendChild('nameFieldText');
xmlData.appendChild(<email/>);
xmlData.email.appendChild('email');
xmlData.appendChild(<contacto/>);
xmlData.contacto.appendChild('phone');
trace(xmlData);
// output is
<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
<pessoa/>
<id>idFieldText</id>
<nome>nameFieldText</nome>
<email>email</email>
<contacto>phone</contacto>
</xmlcontainer>
// And to build on it dont forget to add CDATA tags to all user input fields.<br/>
var xmlData:XML = new XML(<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
</xmlcontainer>
);
var userID:String = '123456789'
var userName:String = 'John doe'
var email:String = '[email protected]'
var phone:String = '888-555-1212'
xmlData.appendChild(<pessoa/>);
xmlData.appendChild(<id/>);
xmlData.id.appendChild( new XML( "\<![CDATA[" + userID + "]]\>" ));
xmlData.appendChild(<nome/>);
xmlData.nome.appendChild( new XML( "\<![CDATA[" + userName + "]]\>" ));
xmlData.appendChild(<email/>);
xmlData.email.appendChild( new XML( "\<![CDATA[" + email + "]]\>" ));
xmlData.appendChild(<contacto/>);
xmlData.contacto.appendChild( new XML( "\<![CDATA[" + userID + "]]\>" ));
trace(xmlData);
//output is
<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
<pessoa/>
<id><![CDATA[123456789]]></id>
<nome><![CDATA[John doe]]></nome>
<email><![CDATA[[email protected]]]></email>
<contacto><![CDATA[123456789]]></contacto>
</xmlcontainer>
// to expand farther and clean up
var xmlData:XML = new XML(<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
</xmlcontainer>
);
var userID:String = '123456789'
var userName:String = 'John doe'
var email:String = '[email protected]'
var phone:String = '888-555-1212'
var client:XML = new XML(<client/>)
//client.appendChild(<id/>);
client.appendChild( new XML( "<id>\<![CDATA[" + userID + "]]\></id>" ));
client.appendChild( new XML( "<nome>\<![CDATA[" + userName + "]]\></nome>" ));
client.appendChild( new XML( "<email>\<![CDATA[" + email + "]]\></email>" ));
client.appendChild( new XML( "<contacto>\<![CDATA[" + userID + "]]\></contacto>" ));
xmlData.appendChild(client);
trace(xmlData);
// output is
<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
<client>
<id><![CDATA[123456789]]></id>
<nome><![CDATA[John doe]]></nome>
<email><![CDATA[[email protected]]]></email>
<contacto><![CDATA[123456789]]></contacto>
</client>
</xmlcontainer>
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.
Upvotes: 1
Reputation: 15717
You can use in the XML { here_my_var_to_replace }
notation for example to replace your dynamic data
Here an example :
function addClient(xml:XML, name:String, phone:String):void {
// will replace into tpl replace {name} and {phone} by their values
var tpl:XML=<client><name>{name}</name><phone>{phone}</phone></client>;
// append the new node to the xml
xml.appendChild(tpl);
}
// test
var myXML:XML=<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
</xmlcontainer>;
addClient(myXML, "foo", "12345678");
trace(myXML.toXMLString());
// output:
<xmlcontainer>
<client>
<name>Marco</name>
<phone>123456789</phone>
</client>
<client>
<name>Roberto</name>
<phone>987654321</phone>
</client>
<client>
<name>foo</name>
<phone>12345678</phone>
</client>
</xmlcontainer>
Upvotes: 0
Reputation: 425
Try:
var xml:XML = <a/>
xml.appendChild(new XML("<b>hello</b>"))
trace(xml.toXMLString());
You should get
<a><b>hello</b></a>
Upvotes: 0