AS3 – XML Include…

A while back now I needed to insert data from one xml file to another at runtime (in flash). After a failed search for something that would do it for me (in AS3 at least), I made this...

XIncludeXMLLoader

In the zip there's a FlashDevelop project set up with an example to get you started. There's no documentation at present, but it's fairly straight forward to use.

You can either insert an entire xml document into another, or select the node(s) you want using an XPath query and only insert that.

Here's an example of some xml you might load...

<data>
<allBooks>
<!-- this will include all data from books.xml -->
<xinclude href="xml/books.xml" query=""/>
</allBooks>

<selectedBooks>
<-- these includes will select certain nodes from books.xml depending on the xpath query conditions -->
<xinclude href="xml/books.xml" query="/bookstore/book[1]/title"/>
<xinclude href="xml/books.xml" query="/bookstore/book/price/text()"/>
<xinclude href="xml/books.xml" query="/bookstore/book[price>35]/price"/>
<xinclude href="xml/books.xml" query="/bookstore/book[price>35]/title"/>
</selectedBooks>
</data>

If an xml file can not be loaded - the 'loaderror' attribute will be added to the xinclude node.

The value of the loaderror attribute indicates the type of error...
<xinclude href="xml/doesnotxist.xml" query="" loaderror="ioError"/>
<xinclude href="xml/self.xml" loaderror="cyclicReference"/>

Have a look at the example and the test xml files in the zip file to get a better idea of what's going on.

Notes:

  • I've not tested this pulling in xml from different domains.
  • I'm using the rather spiffing XPath library from Memorphic for my XPath parsing