简体   繁体   English

在动作脚本中解析XML / XHTML

[英]Parsing XML/XHTML in Actionscript

Is there anything similar to getElementById in actionscript? 动作脚本中是否有类似于getElementById的内容?

I'm trying to make a prototype of a flash page wich gets it's data from a xhtml file. 我正在尝试制作一个Flash页面的原型,以便从xhtml文件中获取数据。 I want to have both an accessible html version (for search engines, textreaders and people without flash) and a flash version (because the customer insists to use flash even though a html-css-ajax solution would do quite nicely). 我想同时拥有可访问的html版本(适用于搜索引擎,文本阅读器和没有Flash的用户)和Flash版本(因为即使HTML-css-ajax解决方案做得很好,客户仍坚持使用Flash)。

What I need is a simple way of getting the text or attributes from the html with a certain id, like <h1 id="flashdataTitle">This is the title</h1> etc. I'm guessing a few ways it might be possible: 我需要的是一种简单的方法,可以从具有特定ID的html中获取文本或属性,例如<h1 id="flashdataTitle">This is the title</h1>等。我猜测可能有几种方法可能:

  • Somehow use an ExternalInterface.call and do the DOM trickery in JavaScript (wich is probably what I will do, because I'm very familiar with JS and a complete newbie with flash/actionscript, unless you have a better solution) 以某种方式使用ExternalInterface.call并在JavaScript中进行DOM欺骗(这可能是我会做的,因为我非常熟悉JS和具有Flash / ActionScript的新手,除非您有更好的解决方案)
  • Load the xhtml with the Actionscript XML class, and manually traverse the XML looking for the correct id attribute (but this is probably very slow) 使用Actionscript XML类加载xhtml,然后手动遍历XML以寻找正确的id属性(但这可能很慢)
  • Use XPath with the XML class in actionscript. 将XPath与ActionScript中的XML类一起使用。 (I'd like some hints on how to do this, if this is the reccomended way) (如果这是推荐的方式,我希望获得有关如何执行此操作的一些提示)
  • There is actually an Actionscript equivalent to getElementById to use with the XML? 实际上有一个与getElementById等效的Actionscript与XML一起使用吗?
  • Allthough my employer hope we don't have to do this: I could rewrite the server side code to output the relevant texts and image urls in a flash-friendly format. 尽管我的雇主希望我们不必这样做:我可以重写服务器端代码,以Flash友好格式输出相关的文本和图像url。

What is the most effective, easiest to implement, and robust-crossbrowser way of doing this? 什么是最有效,最容易实现且健壮的跨浏览器方式? Any totally different ideas? 有完全不同的想法吗?

Please post any ideas even if you think the question have been answered, I'd like to explore all the different possibilities, and allso what disadvantages the proposed solutions have. 即使您认为问题已经解决,也请发表任何想法,我想探索所有不同的可能性,以及所提出的解决方案有哪些缺点。

Since you said your input would be XHTML, you can do it with XPath: 由于您说的输入将是XHTML,因此可以使用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 code example is inspired from here , where you also can find some mode detail on XPath and ActionScript. 该代码示例从此处获得启发,您还可以其中找到有关XPath和ActionScript的一些模式详细信息。

AS3 has it's own XPath Library , the general approach would be the same. AS3拥有自己的XPath库 ,一般方法是相同的。

Is there anything like the prototype.js function inspect() in Actionscript? 是否有类似Actionscript中的prototype.js函数inspect()的东西? I've tried testing the xpath solution but it just won't work. 我已经尝试测试xpath解决方案,但它无法正常工作。 I've tested that the xpath is correct using scetchpad (I think that's what it's called), so I beleave theres a problem with the XML object... It seems to contain the xhtml file when viewing it in the debugger, though it seems quite chaotic, but if I could "inspect" the variables and trace them it would help locating the problem. 我已经使用scetchpad测试了xpath的正确性(我认为这就是所谓的名称),所以我认为XML对象没有问题...在调试器中查看它似乎包含xhtml文件,尽管看起来相当混乱,但是如果我可以“检查”变量并跟踪它们,则将有助于定位问题。 (Thanks Tomalak, I will upvote your answer as soon as my "reputation" is high enough.) (感谢Tomalak,只要我的“声誉”足够高,我就会投票支持你的答案。)

BTW, I still want to hear other ideas. 顺便说一句,我仍然想听听其他想法。

Use XML.idMap (or XMLDocument.idMap in ActionScript 3.0) property if quering elements by id is enough. 如果通过id来查询元素就足够了,请使用XML.idMap(或ActionScript 3.0中的XMLDocument.idMap)属性。 This method is probably fastest way to do this. 此方法可能是最快的方法。 While XPath gives you advanced quering capabilities it reduces performance. 尽管XPath为您提供了高级查询功能,但会降低性能。 So if you need some elements having id attributes I recomend you use idMap. 因此,如果您需要一些具有id属性的元素,我建议您使用idMap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM