Partly support for Flash XML parsing in Adobe Air JavaScript
Working with both Ajax and Flash tecnologies I often miss the great support Actionscript has to the XML format.
Reading the Adobe Air for JavaScript Developer – Pocket Guide I saw some examples of using flash objects in JavaScript. Curious if I also could use the XML parser, I did some testing.
And yes, you can use the parser. But before you can parse it with window.runtime.XML you have to take it via the old window.runtime.flash.xml.XMLDocument() function.
And unfortunately you can call any function on it i.e. XMLlist.length()
Here is the JavaScript code:
<script src="js/AIRAliases.js" type="text/javascript"></script><br><script type="text/javascript"><br> // Var that holds the data loaded<br> var xml_doc;<br> // The function that starts the asynchronously reading of the file<br> function doLoad() { <br> var file = air.File.applicationDirectory.resolvePath('rss.xml' ); <br> stream = new air.FileStream(); <br> stream.addEventListener( air.ProgressEvent.PROGRESS, doProgress ); <br> stream.openAsync( file, air.FileMode.READ ); <br> } <br> // Eventlistener for the fileloading<br> function doProgress( event ) { <br> var data = stream.readMultiByte( stream.bytesAvailable, "utf-8" ); <br> xml_doc += data;<br> if( event.bytesLoaded == event.bytesTotal ) { <br> stream.close();<br> // Call the parse function when fileloading is completed<br> parseXML();<br> } <br> }<br> function parseXML() { <br> var result = new window.runtime.flash.xml.XMLDocument();<br> result.ignoreWhite = true;<br> result.parseXML(xml_doc);<br> var myXML = new window.runtime.XML(result.lastChild);<br> alert(myXML.channel.item[2].title);<br> }<br></script> Why: Today most webservices have a growing support for JSON, but still XML is the most widely spread format. Working with XML files in Javascript can be a little troublesome, and often you want to use an external library for the parsing.