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...
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:












Hi Pi,
The class works like a charm! Thank you very much for this.
I have one remark though, maybe you have an idea. I’d like to be able to remain completely w3c strict and the official declaration of an XInclude element is The issues with this are:
1) How to handle namespaces in AS3… still haven’t figured this one out. somehing like root.leaf::namespace. But thats solvable by bashing…
2) This one is toughter: “include” is a reservered keyword so typing something like root.include.etc is impossible inside AS3. maybe using square bracket notation..?
Anyway thanks.
Sen
Hi littlechicken,
Glad it’s been of use! I had to make a couple of changes to the class earlier this year for that exact reason, but failed to post the update here.
Oops.
The updates are as follows:
- Handles the xi namespace and xi:include nodes (by removing/replacing them at runtime ;0 – i found namespaces in as3 a pain too)
- Change to file path handling. Paths of included files are now relative to the loading xml document, not the Flash or html it’s loaded from.
Get the updated source here (also updated the link the top of the post)
Cheers,
Mike
I popped in the new sourcefiles, works as advertised =] Superchique.
Thanks for the quick response & update. I owe you a beer.
Senne
I like it!