Parsing XML / XHTML in ActionScript
Is there something similar to getElementById in actionscript?
I am trying to prototype a flash page that gets data from an xhtml file. I want to have both an accessible html version (for search engines, text editors and people without flash) and a flash version (because the client insists on using flash, although the html-css-ajax solution would be very good).
I need an easy way to get text or attributes from html with a specific id like <h1 id="flashdataTitle">This is the title</h1>
etc. I assume this is possible:
- Somehow use ExternalInterface.call and do DOM trickery in JavaScript (which is probably what I will do because I'm very familiar with JS and a complete newbie with flash / actionscript if you don't have a better solution)
- Load the xhtml using the Actionscript XML class and manually move the XML looking for the correct id attribute (but this is probably very slow).
- Use XPath with the XML class in ActionScript. (I'd like some hints on how to do this if that's the recommended way)
- Actually the ActionScript equivalent for getElementById for use with XML?
- Even though my employer hopes we don't need to do this: I can rewrite the server-side code to output the appropriate text and image URLs in a flash friendly format.
What is the most efficient, easy-to-use, and reliable cross-browser solution? Any completely different ideas?
Please post any ideas, even if you think the question has been answered, I would like to explore all the different possibilities and all the disadvantages that the proposed solutions have.
source to share
Since you said your input would be XHTML, you can do it with XPath:
import mx.xpath.XPathAPI;
var elementId:String = "flashdataTitle";
var elementPath:String = "//h1[@id'" + elementId + "']";
found_elements = XPathAPI.selectNodeList(xhtml.firstChild, elementPath);
if (found_elements.length == 1) {
trace(found_elements[0]);
}
The sample code is inspired here , where you can also find some mode details in XPath and ActionScript.
AS3 has its own XPath Library , the general approach will be the same.
source to share
Is there something like prototype.js function check () in ActionScript? I tried checking the xpath solution, but it just doesn't work. I tested that the xpath is correct using scetchpad (I think that's what it was calling), so I fear there is a problem with the XML object ... It seems to contain the xhtml file when viewed in the debugger, although it seems quite chaotic. but if I could "check" the variables and keep track of them, that would help find the problem. (Thanks to Tomalak, I will reply to your answer as soon as my "reputation" is high enough.)
By the way, I still want to hear other ideas.
source to share
Use the XML.idMap property (or XMLDocument.idMap in ActionScript 3.0) when specifying query elements by id is sufficient. This method is probably the fastest way to do it. While XPath gives you advanced query capabilities, it does slow performance. So if you need some elements with id attributes, I recommend using idMap.
source to share