To download the code, you must agree to the following license:

Copyright 2010, GreenSock, Inc.

"NO CHARGE" NON-EXCLUSIVE SOFTWARE LICENSE AGREEMENT
-----------------------------------------------------------------------------
PLAIN ENGLISH SUMMARY:

  1. You may use the code at no charge in commercial or non-commercial web sites, games, components, applications, and other software as long as end users are not charged a fee of any kind to use your product or gain access to any part of it. If your client pays you a one-time fee to create the site/product, that's perfectly fine and qualifies under the "no charge" license. If end users are charged a usage/access/license fee, please sign up for a corporate Club GreenSock membership which comes with a special commercial license granting you permission to do so. See http://www.greensock.com/club/ for details.
  2. Use at your own risk. No warranties are offered.
  3. Please respect the copyright.

-----------------------------------------------------------------------------

LEGALESE:

This is a legal agreement between you (either an individual or a single entity) and GreenSock, Inc. ("GREENSOCK") for the proprietary GreenSock ActionScript code known as TweenLite, TweenMax, TweenNano, TimelineLite, TimelineMax, and other code that is available for download at http://www.greensock.com (this code and documentation, as well as any updates which may at GREENSOCK's sole discretion be provided to you from time to time, are referred to in this Agreement as "PROGRAM") By downloading, copying, or otherwise using the PROGRAM, you agree to the terms and conditions of this Agreement. If you do not agree to the terms and conditions of this Agreement, please do not download or use the PROGRAM.

I. LICENSE
A. Subject to the terms and conditions of this Agreement, GREENSOCK hereby grants you a non-exclusive, worldwide, non-transferable right to use the PROGRAM in web sites, games, components and other software applications for which the end user is NOT charged any fees. If you would like to use the code in a commercially licensed software product for which end users are charged a fee (either for usage or access), simply sign up for a corporate Club GreenSock membership at http://www.greensock.com/club/.

II. LIMITATION OF LICENSE AND RESTRICTIONS
A. You agree that you will not disclose, sell, rent, or license the PROGRAM's source code or any derivative works thereof to any third party without the prior written consent of GREENSOCK. Derivative works are defined as modifications that add substantive functionality to the PROGRAM and do not include bug fixes or other minor modifications required to operate the PROGRAM as originally intended. Distribution of the source code as part of your Work Product is acceptable so long as the recipients agree to the terms of this Agreement. You agree not to modify or delete GREENSOCK'S existing copyright notice located in the source code.

B. You may use, duplicate, and distribute the compiled object code as embedded in a Work Product created by you, either for your own use or for distribution to a third party so long as end users of the Work Product are not charged a fee for usage of or access to any portion of the Work Product. Please see http://www.greensock.com/licensing/ for descriptions of Work Products that qualify for the "No Charge" license.

III. CONSIDERATION
A. The license rights granted to you under this Agreement are at no charge, but only in the following circumstances: If on your own behalf or on behalf of a third party you incorporate the PROGRAM into a web site, game, software application, program or any component thereof (collectively, "Work Product"), which in the case of a web site, must be accessible to internet users without payment of a fee of any kind, and in the case of a software application, game, program or component, neither you nor anyone to whom you distribute the Work Product charges a user a fee of any kind to use such Work Product or application, game, program or component into which such Work Product is embedded. The foregoing shall apply regardless of whether you are paid to create such Work Product.

B. In the event your intended use of the PROGRAM does not meet the criteria for the "no charge" license rights set forth in the immediately preceding paragraph, then you are not licensed to use the PROGRAM under this Agreement and must license the PROGRAM under GREENSOCK'S separate fee-based Software License Agreement which is granted to corporate Club GreenSock members (see http://www.greensock.com/club/ for details).

IV. TITLE AND OWNERSHIP
A. The PROGRAM is licensed, not sold, and is protected by copyright laws and international treaty provisions. You acknowledge that no title to the intellectual property in the PROGRAM is transferred to you. You further acknowledge that title and full ownership rights to the PROGRAM, including all intellectual property rights therein, will remain the exclusive property of GREENSOCK and you will not acquire any rights to the PROGRAM except as expressly set forth in this Agreement. You agree that any copies of the PROGRAM you make will contain the same proprietary notices which appear on and in the PROGRAM. You agree that GREENSOCK may identify you as a licensee unless you make a written request otherwise. GREENSOCK hereby grants to you the right to disclose that your product, game, software application, component, or other Work Product makes use of GREENSOCK code (for example, "Powered by TweenLite").

V. DISCLAIMER OF WARRANTY AND LIMITATION OF LIABILITY
A. THE PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. GREENSOCK DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE PROGRAM WILL MEET YOUR REQUIREMENTS OR THAT OPERATION WILL BE UNINTERRUPTED OR ERROR FREE. GREENSOCK shall not be liable for special, indirect, incidental, or consequential damages with respect to any claim on account of or arising from this Agreement or use of the PROGRAM, even if GREENSOCK has been or is hereafter advised of the possibility of such damages. Because some states do not allow certain exclusions or limitations on implied warranties or of liability for consequential or incidental damages, the above exclusions may not apply to you. In no event, however, will GREENSOCK be liable to you, under any theory of recovery, in an amount in excess of $250. Notwithstanding anything else in this agreement, you agree to indemnify GREENSOCK, its assignees, and licensees, and hold each of them harmless from and against any and all claims, demands, losses, damages, liabilities, costs, and expenses, including legal fees resulting from your use of the PROGRAM.

B. GREENSOCK may, at its sole discretion, provide support services related to the PROGRAM, but has no obligation to do so.

VI. TERMINATION
If you at any time fail to abide by the terms of this Agreement, GREENSOCK shall have the right to immediately terminate the license granted herein, require the return or destruction of all copies of the PROGRAM from you and certification in writing as to such return or destruction, and pursue any other legal or equitable remedies available.

VII. MISCELLANEOUS
A. This Agreement shall be construed in accordance with the laws of the State of Illinois. In the event of any dispute between you and GREENSOCK with respect to this Agreement, we both agree that if we cannot resolve the dispute in good faith discussion, either of us may submit the dispute for resolution to arbitration with the American Arbitration Association before a single arbitrator using the AAA Rules for Commercial Arbitration. The arbitrator's decision is final and can be enforced in any court with jurisdiction over such matters.

B. This agreement represents the complete and exclusive statement of the agreement between GREENSOCK and you and supersedes all prior agreements, proposals, representations and other communications, verbal or written, between them with respect to use of the program. This agreement may be modified only with the mutual written approval of authorized representatives of the parties.

C. The terms and conditions of this Agreement shall prevail notwithstanding any different, conflicting, or additional terms or conditions which may appear in any purchase order or other document submitted by you. You agree that such additional or inconsistent terms are deemed rejected by GREENSOCK.

D. GREENSOCK and you agree that any xerographically or electronically reproduced copy of this Agreement shall have the same legal force and effect as any copy bearing original signatures of the parties.

I'd like to learn how to get bonus plugins, update notifications, SVN access, and more.
To join Club GreenSock, you must agree to the following license:

XMLParser – Painless XML Translation

  • Version: 4.82, Updated 8/26/2008
  • Compatibility: Flash Player 6 and later (AS2)
  • File size added to compressed SWF: about 3k

Download Now
  Donate

NOTE: If you need an AS3 version of this class, you can get it HERE (although the way AS3 handles XML makes this class largely unnecessary)

Visit the new forums

Description

This class provides an easy way to load and/or send an XML file and parse the data into a format that's simple to work with. Every node becomes an array with the same name. All attributes are also easily accessible because they become properties with the same name. So for example, if this is your XML:

<Resources>
<Book name="Mary Poppins" ISDN="1122563" />
<Book name="The Bible" ISDN="333777" />
<Novel name="The Screwtape Letters" ISDN="257896">
<Description>This is an interesting perspective</Description>
</Novel>
</Resources>

Then you could access the first book's ISDN with:

Actionscript:
  1. Book[0].ISDN

The value of a node (like the text between the <Description> and </Description> tags above) can
be accessed using the "value" property, like so:

Actionscript:
  1. Novel[0].Description[0].value

Just remember that all nodes become arrays even if there's only one node, and attributes become properties. You can obviously loop through the arrays too which is very useful. The root node is ignored for efficiency (less code for you to write) unless you set the keepRootNode_bol parameter to true. Also, numeric values are properly translated into numbers, not strings (as of version 4.6)

OBJECTIVES

  • Eliminate the hassle and complexity of searching through XML using methods like my_xml.firstChild.firstChild.childNodes[2].attributes.name.
  • Create an intuitive way to organize XML data while maintaining its heirarchy and naming conventions.
  • Provide a mechanism to both parse XML data into an ActionScript-friendly format and also take an ActionScript object and translate it into an XML object (maybe for sending to a server).

Important properties

  • percentLoaded_num: Poll this value to find out the loading progress
  • bytesLoaded
  • bytesTotal
  • xml
  • loaded_bol
  • keepRootNode_bol

Usage

XMLParser.load(url:String, onComplete:Function, parsedObject:Object, keepRootNode:Boolean, parseLineBreaks:Boolean):XMLParser
  • Description: A static function that loads an XML document from a url (relative or absolute), parses it into the parsedObject, and then calls the onComplete (if you define one)
  • Arguments:
    1. url: The URL (relative or absolute) where the XML file resides that you'd like to parse
    2. onComplete: [optional] A reference to a function that you'd like to call as soon as the XML file has been loaded and parsed. Three parameters will get passed to that function in the following order:
      1. success: If the XML file was loaded successfully, this will be true. Otherwise, it's false.
      2. parsedObject: The object containing all of the parsed XML data
      3. original_xml: The original XML that was loaded (unparsed)
    3. parsedObject: [optional] A reference to an object that should be populated with the parsed XML data.
    4. keepRootNode: [optional] By default, the class essentially ignores the root node to make it easier for you to access data (shorter code), but if you want to keep that root node, set this value to true. Obviously it's false by default.
    5. parseLineBreaks: [optional] If you set this value to true, all instances of "\n" in your XML will be parsed and replaced with line breaks in Flash.

XMLParser.sendAndLoad(toSend:Object, url:String, onComplete:Function, parsedObject:Object, keepRootNode:Boolean, parseLineBreaks:Boolean):XMLParser
  • Description: A static method that translates an ActionScript object into XML format and sends it to a url and then returns any resulting XML (from the server) to the onComplete function.
  • Arguments:
    1. toSend: The ActionScript object that you'd like to send to the server. It does NOT need to be a simple object with only a few properties. Its properties can contain arrays (with other arrays, etc.)
    2. url: The url to which the XML should be sent.
    3. onComplete: [optional] A reference to a function that you'd like to call as soon as the XML file has been and the server has responded and the resulting XML has been parsed. Three parameters will get passed to that function in the following order:
      1. success: If the XML file was loaded successfully, this will be true. Otherwise, it's false.
      2. parsedObject: The object containing all of the parsed XML data
      3. original_xml: The original XML that was loaded (unparsed)
    4. parsedObject: [optional] A reference to an object that should be populated with the resulting parsed XML data.
    5. keepRootNode: [optional] By default, the class essentially ignores the root node to make it easier for you to access data (shorter code), but if you want to keep that root node, set this value to true. Obviously it's false by default.
    6. parseLineBreaks: [optional] If you set this value to true, all instances of "\n" in your XML will be parsed and replaced with line breaks in Flash.

XMLParser.objectToXML(toTranslate:Object, rootNodeName:String):XML
  • Description: A static function that translates any ActionScript object into an XML object. It will retain the name of each property (which ends up being a node attribute) and array (which ends up being a collection of nodes).
  • Arguments:
    1. toTranslate: The ActionScript object that should be translated into XML.
    2. rootNodeName: [optional] An XML document must have a single root node. You can define that name here. "XML" is the default name.

XMLParser.XMLToObject(xml:XML, parsedObject:Object, keepRootNode:Boolean, parseLineBreaks:Boolean):Object
  • Description: A static function that translates any XML into an object. It will retain the name of each attribute (which ends up being a property) and node value (which ends up being the "value" property of the resulting object). Similarly named node siblings are automatically dumped into an array.
  • Arguments:
    1. xml: The XML that should be translated into an object.
    2. parsedObject: [optional] If you want to add the properties/arrays to a particular object that you've already created, pass it in here, otherwise a new object will get returned with all of the properties/arrays from the parsed XML.
    3. keepRootNode: [optional] By default, the class essentially ignores the root node to make it easier for you to access data (shorter code), but if you want to keep that root node, set this value to true. Obviously it's false by default.
    4. parseLineBreaks: [optional] If true, all instances of "\n" in your XML will be parsed and replaced with line breaks in Flash

cancel()
  • Description: Stops loading the XML document

destroy()
  • Description: Destroys the instance (and cancels loading the XML)

Examples

To simply load a "myDocument.xml" document and parse the data into ActionScript-friendly values, do:

Actionscript:
  1. import gs.dataTransfer.XMLParser;
  2. XMLParser.load("myDocument.xml", onFinish);
  3. function onFinish($success:Boolean, $results:Object, $xml:XML):Void { //This function gets called when the XML gets parsed.
  4.     if ($success) {
  5.         trace("The first book is: "+$results.Book[0].name);
  6.     }
  7. }

Or to send an object to the server in XML format (remember, each element in an array becomes a node and all
object properties become node attributes) and load the results back into an ActionScript-friendly format, do:

Actionscript:
  1. import gs.dataTransfer.XMLParser;
  2. //Create an object to send an populate it with values...
  3. var toSend_obj = new Object();
  4. toSend_obj.name = "Test Name";
  5. toSend_obj.Book = new Array();
  6. toSend_obj.Book.push({title:"Mary Poppins", ISDN:"125486523"});
  7. toSend_obj.Book.push({title:"The Bible", ISDN:"25478866998"});
  8. //Now send the data and load the results from the server into the response_obj...
  9. XMLParser.sendAndLoad(toSend_obj, "http://www.myDomain.com/myScript.php", onFinish);
  10. function onFinish($success:Boolean, $results:Object, $xml:XML):Void {
  11.     if ($success) {
  12.         trace("The server responded with this XML: " + $xml);
  13.         trace("The server's response was translated into this ActionScript object: " + $results);
  14.     }
  15. }

In the above example, the server would receive the following XML:

<XML name="Test Name">
	<Book ISDN="125486523" title="Mary Poppins" />
	<Book ISDN="25478866998" title="The Bible" />
</XML>

Need help?

Feel free to post your question on the forums. You'll increase your chances of getting a prompt answer if you provide a brief explanation and include a simplified FLA file (and any class files) that clearly demonstrates the problem.

Donate
Author: Jack Doyle
Copyright 2010, GreenSock (This work is subject to the terms here.)

Comments (34) RSS

Posted by Brian on April 4, 2007

This looks great. How’s the performance?

Posted by jack on April 4, 2007

Brian, I’ve been using this for quite some time and have never had any performance problems whatsoever, but I haven’t tried pushing it to its limits either by loading a 10MB XML file or anything. Nobody has reported performance problems either, so I think it’s pretty safe to say that it’ll perform quite well for you unless you get crazy with the amount XML data you load which would be more of a Flash limitation than this class.

Posted by Clone on August 16, 2007

Great class, just the thing i’ve been searching for.
Hope i’ll be able to put it in good use.

Cheers!

Posted by Brendan on August 20, 2007

This is a great class, but very annoying at the same time. For example, if I already have an XML file loaded into my movie, I cannot convert it to an object using this class. It only works if I want to load XML from an external file.

Is there a way to convert an already-loaded XML file into an object using this class?

Posted by jack on August 21, 2007

Brendan, you’ve got a valid point. I’ve never run into a situation where I’d want or need to load in XML without using XMLParser, but maybe you’re working with XML that’s being created from within an SWF in which case it’d be handy to have an XMLToObject() method. Wish granted. I just updated the class (v4.0) to allow for this. It should be as simple as passing your XML into the static XMLParser.XMLToObject() method and you’ll get an object returned. Enjoy.

Posted by David on September 21, 2007

Great class. It is very useful, and painless to use. I have a question…

Using the XMLParser.load() method, how would you reference the attributes of the first node?

How would I reference the “id” attribute in the following example?

Thanks!

Posted by jack on September 21, 2007

David, as of version 4.4, you can now define a keepRootNode_bol parameter as true to force the class to include the root node. Keep in mind it’s false by default. So an example would be:

XMLParser.load(“myDocument.xml”, onCompleteLoad, results_obj, true);

The new version also adds the ability to get the bytesLoaded, bytesTotal, and percentLoaded_num for preloader visualization.

Enjoy!

Posted by George on September 30, 2007

You totally rock! This is a wonderful timesaver and it works flawlessly. Thank you!

Posted by Ricardo on October 19, 2007

yo dude Thanks a million for these classes! and most of all THANKS for sharing them for free! this is really a good attitude.

have a nice one!

Posted by Charles on October 20, 2007

Thank you very, very much for these classes ! They are incredibly useful to me !

Posted by pista on October 29, 2007

this is just great, exactly what I was looking for…
thank you very much

Posted by michako on November 29, 2007

thanks for your classes Jack!
you are a time saver.
question: is there a possibility to make XMLParser for actionscript 3??
that would be ideal!

Posted by jack on November 29, 2007

Michako, yes, I do plan on eventually porting XMLParser to AS3 but not until I have a project that requires it. I’ve got too much on my plate right now to attack it, but stay tuned…

Posted by Tilman on December 22, 2007

How do I use cancel()? XMLParser.cancel() does not work, because it is not a static method. But XMLParser.load() is static, so this is how it should be used, right?

I would like to kill the loading of the first XML, when the user clicks fast enough to fetch a second XML before the first is loaded.

Can you give a hand?

Posted by jack on December 22, 2007

Tilman, the static load() method returns an instance of the XMLParser class, and that’s what you can call cancel() on. So you could do:

var myXMLParser = XMLParser.load(“myXML.xml”, onFinish);

//Then, to cancel, you’d do:
myXMLParser.cancel();

Posted by brice_m_g on January 10, 2008

thanx,

very usefull class.

what about special characters : “é”, “è” or “à” ?

does it work ?

bmg///

Posted by jack on January 14, 2008

brice_m_g, from my testing, in order to get those special characters to show up inside Flash, you must:

1) Put this code on the first frame of your SWF: System.useCodepage = true;

2) Make sure those characters are embedded in the TextFields where you’ll use them.

Posted by Og2t on January 30, 2008

Hey Jack! Your class is great! I was using Zeh Fernando’s XML2Object before but I think I’ll stick to yours from now on.

I thought about one little helpful feature though: while parsing XML document, you could change the type of the value/attribute to Number (instead of being String) when it seems to be numeric.

I found it useful while parsing XML data describing Tweens, using String type values didn’t work and it took me a while to figure it out why.

Posted by jack on January 30, 2008

Great suggestion, Og2t. As of version 4.6, XMLParser will parse numbers as numbers, not strings.

Posted by Paul on February 22, 2008

You sir, are a God amongst Men!

Thanks Jack!

Posted by Paul Godfrey on July 12, 2008

The best parser I’ve seen on the net and I’ve been around since the ice age. Very good work Jack I hope you can find the time to maintain the project.

Posted by Stathis on September 16, 2008

jack,
This is the best xml related class i’ve seen!!
Congratulations and thanks for sharing!

Posted by Carl Prescott on September 23, 2008

Fantastic!

Posted by fer on October 21, 2008

thanks jack… now i’m beginning to love XML ^^

Posted by laura on February 18, 2009

any chance someone out there knows if this parses html text ? I’m trying to use cdata but have not been successful so far, any thoughts?

Thanks Jack, I love this parser, makes my life so much easier!

best,
~laura

Posted by laura on February 18, 2009

Awsome, I got it to work. there’s two ways to put in html links
without CDATA, using utf-8 characters,

<a href="http://www.google.com/"> Link to Google</a>”

and the other with CDATA would be

<![CDATA[ Link to Google]]>

Thanks again Jack!

Posted by Mark on April 15, 2009

Hi Jack,

parser works great for me. Got a 1.5 Mb XML to load. Is there a way to monitor the loading progress?

thanks!

Posted by jack on April 15, 2009

Sure, Mark – there are “bytesLoaded” and “bytesTotal” properties you can tap into as well as a “percentLoaded” property (“percentLoaded_num” in AS2). Keep in mind that the load() method returns an instance that you can use to check those values.

Posted by John on May 18, 2009

Been using the TweenLite engine for a while, just found out about this.

Terrific work. You are a real asset.

Posted by Josh L on June 1, 2009

I migrated from Flash to Javascript (now doing full time JS development) a while back. This week, however, I need to create an API for an XML web service for use in Flash.

I just got headaches from trying to use Flash’s built in web service tools, as our web service wasn’t using any kind of premade toolkit or standards (it’s pretty janky but it works, lol). I then decided to use my old friend XMLParser and just build the API on top of that.

Best decision ever. This thing is so damn versatile! I think I’ll have to donate again tonight.

Posted by Shawn on September 4, 2009

I am new to AS3 and learning. I just wanted to say thank you for all these wonderful classes you are allowing us to download. I came here to try out TweenMax (having had some success with Tweener) and I stumbled upon these other projects of yours. They look great and I only wish I had more time to try them out.

Anyway, thanks for making them accessible! Once I understand things better I’m sure I’ll want to join your club and contribute to your work financially. (In faith, I’ll start saving my pennies)

Thanks again!

Posted by Samuel on November 8, 2009

Jack, you are Timer saver for us. Thank you for all wonderful classes.

Posted by onegiantmedia on December 3, 2009

I’ve said it before, and I’ll say it again.

YOU ARE THE MAN!

Posted by Julien on February 3, 2010

Thanks a lot for that class !

It seems that you changed the “value” proprety to “nodeValue” where we get the value of a node.

It tooks me a while to understand why I was getting all those undefined values !

Thanks again for the class !