在Actionscript中解析XML / XHTML [英] Parsing XML/XHTML in Actionscript

查看:146
本文介绍了在Actionscript中解析XML / XHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在actionscript中有没有类似getElementById的东西?

我试图制作一个flash页面的原型,以便从xhtml文件获取数据。我想同时拥有一个可访问的html版本(用于搜索引擎,文本阅读器和没有flash的用户)和一个flash版本(因为即使html-css-ajax解决方案能够很好地完成,客户坚持使用flash)。

我需要的是一个简单的方法,从一个特定的id中获取html文本或属性,比如< h1 id =flashdataTitle >这是标题< / h1> 等我猜有几种方式可能:


    'LI>不知怎的,使用ExternalInterface.call,做DOM弄虚作假在JavaScript(至极大概是我会做什么,因为我非常熟悉JS用闪光灯和/动作一个完整的新手,除非你有一个更好的解决方案)
  • 使用Actionscript XML类加载xhtml,并手动遍历XML查找正确的id属性(但这可能非常缓慢)
  • 在actionscript中使用带有XML类的XPath。 (我想就如何做到这一点的一些提示,如果这是reccomended方式)

  • 有实际上相当于的getElementById一个ActionScript与XML?
  • $使用b $ b
  • 虽然我的雇主希望我们不必这样做:我可以重写服务器端代码,以闪存友好的格式输出相关的文本和图像url。


什么是最有效,最容易实现的方法,以及强大的交叉浏览器的方式?任何完全不同的想法?



即使您认为问题已经回答,请发表任何想法,我想探索所有不同的可能性,以及所有的缺点提出的解决方案有。

解决方案

因为你说你的输入是XHTML,你可以用XPath来完成: b
$ b

  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]);





代码示例是从此处,在这里你还可以找到有关XPath和ActionScript一些模式的细节。



AS3拥有自己的 XPath Library ,一般的做法是一样的。


Is there anything similar to getElementById in actionscript?

I'm trying to make a prototype of a flash page wich gets it's data from a xhtml file. 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).

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:

  • 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)
  • 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)
  • Use XPath with the XML class in actionscript. (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?
  • 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.

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:

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.

AS3 has it's own XPath Library, the general approach would be the same.

这篇关于在Actionscript中解析XML / XHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆