Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Actionscript 3 writing a XML element inside a another with code
- <xmlcontainer>
- <client>
- <name>Marco</name>
- <phone>123456789</phone>
- </client>
- <client>
- <name>Roberto</name>
- <phone>987654321</phone>
- </client>
- </xmlcontainer>
- 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" );
- }
- var xml:XML = <a/>
- xml.appendChild(new XML("<b>hello</b>"))
- trace(xml.toXMLString());
- <a><b>hello</b></a>
- 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>
- 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 = 'my@email.com'
- 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[my@email.com]]></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 = 'my@email.com'
- 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[my@email.com]]></email>
- <contacto><![CDATA[123456789]]></contacto>
- </client>
- </xmlcontainer>
Add Comment
Please, Sign In to add comment