Parsing an XML Packet in Flash

June 19th, 2006 by Posted in Macromedia Flash, Macromedia Products

Parsing an XML Packet in Flash

  1. // if the XML packet was a simple packet using attributes.
  2. var login_xml:XML = new XML(”<?xml version=”1.0″ ?><login status=”success” sessionid=”57754B095346D64A” />”);
  3. trace(”status: “+login_xml.firstChild.attributes.status);
  4. trace(”sessionid: “+login_xml.firstChild.attributes.sessionid);
  5. trace(”");
  6. // if the XML packet had child nodes.
  7. var login_xml:XML = new XML(”<?xml version=”1.0″ ?><login><status>success</status><sessionid>57754B095346D64A</sessionid></login>”);
  8. trace(”status: “+login_xml.firstChild.childNodes[0].firstChild.nodeValue);
  9. trace(”sessionid: “+login_xml.firstChild.childNodes[1].firstChild.nodeValue);
  10. trace(”");
  11. // parse XML packet using a for loop to loop over the childNodes
  12. var login_xml:XML = new XML(”<?xml version=”1.0″ ?><login><status>success</status><sessionid>57754B095346D64A</sessionid></login>”);
  13. for (var i = 0; i<login_xml.firstChild.childNodes.length; i++) {
  14.     trace(login_xml.firstChild.childNodes[i].nodeName+”: “+login_xml.firstChild.childNodes[i].firstChild.nodeValue);
  15. }