XML is robust, structured, easy to understand, and inherently cross-platform cross-application data transmission. Flex and as support the XML syntax of E4X, making XML operations extremely convenient and simple. The flexible use of XML often makes our work even more powerful. The following is a summary of the XML operations in flex.
1. Create an XML object:
1.1 directly write the code on the right of the equal sign and assign the value to the XML variable:
VaR XML: xml = <Student name = "Mary" Sex = "female"/>
Fill XML with variables:
VaR name: String = "Mary"; <br/> var sex: String = "female"; <br/> var XML: xml = <Student name = {name} sex = {sex}/>;
1.2 pass the XML structure string as a parameter to the XML constructor to return the XML object:
VaR XML: xml = New XML ('<Student name = "Mary" Sex = "female"/> ');
1.3 load XML objects from external sources:
// Load the weather forecast RSS for the next 7 days in Beijing Haidian <br/> var Loader: urlloader = new urlloader (); <br/> loader. dataformat = "E4X"; <br/> loader. addeventlistener (event. complete, oncomplete); <br/> loader. load (New URLRequest ("http://weather.raychou.com /? /Detail/54399/RSS "); <br/> private function oncomplete (Event: Event): void {<br/> var weather: xml = new xml(event.tar get. data); <br/>}
2. Read XML data:
// Construct an example XML Object <br/> var employees: xml = <employees> <br/> <employee name = "Christina coenraets" active = "true"> <br/> <phone> 555-219-2270 </phone> <br/> <email> ccoenraets@fictitious.com </Email> <br/> <active> true </active> <br/> </employee> <br/> <employee name =" joanne wall "active =" false "> <br/> <phone> 555-219-2012 </phone> <br/> <email> jwall@fictitious.com </Email> <br/> <other> <br/> <email> jwall@gmail.com </Email> <br/> </other> <br/> </employee> <br/> </employees>;
// Traverse the child nodes of XML <br/> for each (var employee: XML in employees. elements () {<br/> trace (employee. toxmlstring () <br/>}< br/> var employee1: xml = employees. elements () [0]; // The first child element of employees <br/> var parent: xml = employee1.parent (); // read the XML parent node <br/> // use the "@" operation to read the XML attributes <br/> trace (employee1. @ name ); // Christina coenraets <br/> // use the "attribute (attr_name)" function to read XML attributes <br/> trace (employee1.attribute ("active ")); // true <br/> // use the "[name]" operation to read all nodes named "email" and return the xmllist object <br/> var Emails: xmllist = employees. employee ["email"]; <br/> trace (emails. length () // 2 <br/> // use ". "The operation reads all nodes named" email "and returns the xmllist object <br/> var theemails: xmllist = employees. employee. email; <br/> trace (theemails. length () // 2 <br/> // use ".. "The operation reads all nodes with the depth of email, and the range is xmllist object <br/> var alleamils: xmllist = employees .. email; <br/> // use the "Child (name)" function to read all nodes named "phone" and return the xmllist object <br/> var phones: xmllist = employees. employee. child ("phone"); <br/> trace (alleamils. length () // 3
3. Add XML subnodes and attributes:
VaR EMPLOYEE: xml = <employee/>; <br/> // Add attributes <br/> employee. @ ID = "1314"; <br/> // assign a value using a string and add a subnode <br/> employee. email = "jwall@fictitious.com"; <br/> // use the "appendchild (object)" function to insert a subnode at the end <br/> employee. appendchild (<sex> male </sex>); <br/> // use the "prependchild (object)" function to insert a subnode in the header <br/> employee. prependchild (<Name> Joanne wall </Name>); <br/> // assign values with an XML Object and add sub-nodes <br/> employee. nationality = <nationality> USA </nationality>; <br/> // use the "insertchildafter (object1, object2)" function to insert a subnode after object1 <br/> employee. insertchildafter (employee. child ("name"), <active> false </active>); <br/> // use the "insertchildbefore (object1, object2)" function, insert a subnode before object1 <br/> employee. insertchildbefore (employee. child ("active"), <phone> 555-219-2012 </phone>); <br/> trace (employee. toxmlstring () <br/>/** <br/> output result: <br/> <employee ID = "1314"> <br/> <Name> Joanne wall </Name> <br/> <phone> 555-219-2012 </phone> <br/> <active> false </active> <br/> <email> jwall@fictitious.com </Email> <br/> <sex> male </sex> <br/> <nationality> USA </nationality> <br/> </employee> <br/> */
4. Delete XML subnodes and attributes:
VaR employee: xml = <employee ID = "1314" name = "Joanne wall"> <br/> <phone> 555-219-2012 </phone> <br/> <email> jwall@fictitious.com </Email> <br/> </employee>; <br/> Delete employee. @ ID; <br/> Delete employee. phone; <br/> trace (employee. toxmlstring (); <br/>/** output result: <br/> <employee name = "Joanne wall"> <br/> <email> jwall@fictitious.com </Email> <br/> </employee> <br/> */
5. Search for a specified node:
VaR employees: xml = <employees> <br/> <employee ID = "13"> <br/> <Name> Christina coenraets </Name> <br/> <phone> 555-219 -2270 </phone> <br/> <email> ccoenraets@fictitious.com </Email> <br/> </employee> <br/> <employee ID = "14"> <br /> <Name> Joanne wall </Name> <br/> <phone> 555-219-2012 </phone> <br/> <email> jwall@fictitious.com </Email> <br/> <other ID = "2012"/> <br/> </employee> <br/> </employees>; <br/> // subnode with a property ID of 13 <br/> var CHRISTINA: xml = employees. employee. (@ ID = 13) [0]; <br/> // subnode whose name element is "Joanne wall" <br/> var Joanne: xml = employees. employee. (name = "Joanne wall") [0]; <br/> var other: xml = employees .. *. (hasownproperty ("@ ID") & @ ID = "2012") [0]; // <other ID = "2012"/>