本文介绍了在SWF AS3中,如何从网站中提取字符串内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SWF AS3中,如何从网站中提取字符串内容?

In swf AS3, how do you extract string content from a website?

在html网页中,它们显示一行文本.在swf中,我想使用as3通过其URL访问该页面并检索该行.是访问内容的一种方法吗?

In a html web page, they display a single line of text. In the swf, I want to use as3 to access that page by its URL and retrieve that line. Is the a way to access the content?

提前感谢您的帮助!

推荐答案

您只需加载文件并阅读即可.假设您在domain.com/test上有此HTML:

You simply load the file and read it. Say you have this HTML located at domain.com/test:

<!DOCTYPE html>
<html>
    <body>
        <div class="target">Get this text</div>
    </body>
</html>

您将使用Flash中的 URLLoader 加载它:

You would load it in using a URLLoader in Flash:

var l:URLLoader = new URLLoader();
l.addEventListener(Event.COMPLETE, completeHandler);
l.load(new URLRequest("domain.com/test"));

这将通过 Event.target.data 将上述HTML作为 String 加载到 completeHandler 中.然后,您可以做两件事:通过RegEx搜索字符串或通过将HTML设置为 XML 对象来搜索它.

That will load the above HTML in as a String in the completeHandler via Event.target.data. You can then do two things: Search for your string via RegEx or search for it by setting the HTML as an XML object.

使用RegEx,您可以这样做:

Using RegEx, you would do it like this:

function completeHandler(e:Event):void {
    var s:String = e.target.data;
    var targets:Array = s.match(/(?<=<div class="target">).*(?=<\/div>)/igm);
}

targets 将是一个类名为"target"的div的匹配数组.

targets would be an array of matches for a div with class name "target".

XML 会有点困难,但无限灵活且易于维护(至少在我看来).我将不提供有关如何执行此操作的示例,因为解析XML是AS3中非常普遍的事情,并且还有很多其他问题.

XML would be a bit more difficult, but infinitely more flexible and easy to maintain (in my opinion, at least). I won't give an example for how to do that, since parsing XML is a very common thing in AS3 and there are tons of other questions out there about it.

希望有帮助

这篇关于在SWF AS3中,如何从网站中提取字符串内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 03:59