Guest User

Untitled

a guest
Aug 23rd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. Actionscript 3 writing a XML element inside a another with code
  2. <xmlcontainer>
  3. <client>
  4. <name>Marco</name>
  5. <phone>123456789</phone>
  6. </client>
  7. <client>
  8. <name>Roberto</name>
  9. <phone>987654321</phone>
  10. </client>
  11. </xmlcontainer>
  12.  
  13. function appendXML():void{
  14.  
  15. var xmlData:XML = new XML();
  16. var xmlrequest:URLRequest = new URLRequest(String("xml/clientelist.xml"));
  17. xmlLoader.load(xmlrequest);
  18. xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
  19.  
  20. function LoadXML(e:Event):void
  21. {
  22. xmlData = new XML(e.target.data);
  23. xmlData.appendChild(<pessoa/>);
  24. xmlData.appendChild(<id/>);
  25. xmlData.id.appendChild(idfield.text);
  26. xmlData.appendChild(<nome/>);
  27. xmlData.nome.appendChild(nomefield.text);
  28. xmlData.appendChild(<email/>);
  29. xmlData.email.appendChild(emailfield.text);
  30. xmlData.appendChild(<contacto/>);
  31. xmlData.contacto.appendChild(contacto1field.text);
  32. trace(xmlData);
  33.  
  34. var fileb:FileReference = new FileReference;
  35. fileb.save( xmlData, "clientelist.xml" );
  36. }
  37.  
  38. var xml:XML = <a/>
  39. xml.appendChild(new XML("<b>hello</b>"))
  40. trace(xml.toXMLString());
  41.  
  42. <a><b>hello</b></a>
  43.  
  44. function addClient(xml:XML, name:String, phone:String):void {
  45. // will replace into tpl replace {name} and {phone} by their values
  46. var tpl:XML=<client><name>{name}</name><phone>{phone}</phone></client>;
  47.  
  48. // append the new node to the xml
  49. xml.appendChild(tpl);
  50. }
  51.  
  52. // test
  53.  
  54. var myXML:XML=<xmlcontainer>
  55. <client>
  56. <name>Marco</name>
  57. <phone>123456789</phone>
  58. </client>
  59. <client>
  60. <name>Roberto</name>
  61. <phone>987654321</phone>
  62. </client>
  63. </xmlcontainer>;
  64.  
  65. addClient(myXML, "foo", "12345678");
  66.  
  67. trace(myXML.toXMLString());
  68. // output:
  69. <xmlcontainer>
  70. <client>
  71. <name>Marco</name>
  72. <phone>123456789</phone>
  73. </client>
  74. <client>
  75. <name>Roberto</name>
  76. <phone>987654321</phone>
  77. </client>
  78. <client>
  79. <name>foo</name>
  80. <phone>12345678</phone>
  81. </client>
  82. </xmlcontainer>
  83.  
  84. var xmlData:XML = new XML(<xmlcontainer>
  85. <client>
  86. <name>Marco</name>
  87. <phone>123456789</phone>
  88. </client>
  89. <client>
  90. <name>Roberto</name>
  91. <phone>987654321</phone>
  92. </client>
  93. </xmlcontainer>
  94. );
  95. xmlData.appendChild(<pessoa/>);
  96. xmlData.appendChild(<id/>);
  97. xmlData.id.appendChild('idFieldText');
  98. xmlData.appendChild(<nome/>);
  99. xmlData.nome.appendChild('nameFieldText');
  100. xmlData.appendChild(<email/>);
  101. xmlData.email.appendChild('email');
  102. xmlData.appendChild(<contacto/>);
  103. xmlData.contacto.appendChild('phone');
  104. trace(xmlData);
  105.  
  106. // output is
  107. <xmlcontainer>
  108. <client>
  109. <name>Marco</name>
  110. <phone>123456789</phone>
  111. </client>
  112. <client>
  113. <name>Roberto</name>
  114. <phone>987654321</phone>
  115. </client>
  116. <pessoa/>
  117. <id>idFieldText</id>
  118. <nome>nameFieldText</nome>
  119. <email>email</email>
  120. <contacto>phone</contacto>
  121. </xmlcontainer>
  122.  
  123. // And to build on it dont forget to add CDATA tags to all user input fields.<br/>
  124. var xmlData:XML = new XML(<xmlcontainer>
  125. <client>
  126. <name>Marco</name>
  127. <phone>123456789</phone>
  128. </client>
  129. <client>
  130. <name>Roberto</name>
  131. <phone>987654321</phone>
  132. </client>
  133. </xmlcontainer>
  134. );
  135. var userID:String = '123456789'
  136. var userName:String = 'John doe'
  137. var email:String = 'my@email.com'
  138. var phone:String = '888-555-1212'
  139.  
  140. xmlData.appendChild(<pessoa/>);
  141. xmlData.appendChild(<id/>);
  142. xmlData.id.appendChild( new XML( "<![CDATA[" + userID + "]]>" ));
  143. xmlData.appendChild(<nome/>);
  144. xmlData.nome.appendChild( new XML( "<![CDATA[" + userName + "]]>" ));
  145. xmlData.appendChild(<email/>);
  146. xmlData.email.appendChild( new XML( "<![CDATA[" + email + "]]>" ));
  147. xmlData.appendChild(<contacto/>);
  148. xmlData.contacto.appendChild( new XML( "<![CDATA[" + userID + "]]>" ));
  149. trace(xmlData);
  150.  
  151. //output is
  152. <xmlcontainer>
  153. <client>
  154. <name>Marco</name>
  155. <phone>123456789</phone>
  156. </client>
  157. <client>
  158. <name>Roberto</name>
  159. <phone>987654321</phone>
  160. </client>
  161. <pessoa/>
  162. <id><![CDATA[123456789]]></id>
  163. <nome><![CDATA[John doe]]></nome>
  164. <email><![CDATA[my@email.com]]></email>
  165. <contacto><![CDATA[123456789]]></contacto>
  166. </xmlcontainer>
  167.  
  168. // to expand farther and clean up
  169.  
  170. var xmlData:XML = new XML(<xmlcontainer>
  171. <client>
  172. <name>Marco</name>
  173. <phone>123456789</phone>
  174. </client>
  175. <client>
  176. <name>Roberto</name>
  177. <phone>987654321</phone>
  178. </client>
  179. </xmlcontainer>
  180. );
  181. var userID:String = '123456789'
  182. var userName:String = 'John doe'
  183. var email:String = 'my@email.com'
  184. var phone:String = '888-555-1212'
  185.  
  186.  
  187.  
  188. var client:XML = new XML(<client/>)
  189. //client.appendChild(<id/>);
  190. client.appendChild( new XML( "<id><![CDATA[" + userID + "]]></id>" ));
  191. client.appendChild( new XML( "<nome><![CDATA[" + userName + "]]></nome>" ));
  192. client.appendChild( new XML( "<email><![CDATA[" + email + "]]></email>" ));
  193. client.appendChild( new XML( "<contacto><![CDATA[" + userID + "]]></contacto>" ));
  194.  
  195. xmlData.appendChild(client);
  196.  
  197. trace(xmlData);
  198.  
  199. // output is
  200. <xmlcontainer>
  201. <client>
  202. <name>Marco</name>
  203. <phone>123456789</phone>
  204. </client>
  205. <client>
  206. <name>Roberto</name>
  207. <phone>987654321</phone>
  208. </client>
  209. <client>
  210. <id><![CDATA[123456789]]></id>
  211. <nome><![CDATA[John doe]]></nome>
  212. <email><![CDATA[my@email.com]]></email>
  213. <contacto><![CDATA[123456789]]></contacto>
  214. </client>
  215. </xmlcontainer>
Add Comment
Please, Sign In to add comment