我有一个基于时间显示的各种拍卖项目的XML文件。我想根据XML文件字段中设置的小时使用AS3来获取getUTCDate,然后显示相应的XML节点。我几乎是AS3 + XML的中间人,所以我可以使用一些帮助。我已经完成了所有设置,但有些事情并不完全正确。
AS3代码:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:Number = my_date.getUTCHours() + (my_date.getUTCMinutes() / 60);
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
for(var i:uint = 0; i < xmlData.auction.length(); i++)
{
if(xmlData.auction[i].time < currentHour)
{
auctionName_txt.text = xmlData.auction[i].title;
auctionDesc_txt.text = xmlData.auction[i].description;
}
}
}
我是否需要从xml中声明各种时间,例如每次或每次做一次var?我如何得到它,以便AS3知道时间节点是实际时间?这是一些示例XML代码。 (我也没有调用图像,但我只是试图让这个东西首先读取相应的节点。)它不会抛出编译器错误,但它也不确定该怎么做。
XML代码:
<auctionlist>
<auction>
<time>6:00</time>
<title>Example Auction 1</title>
<description>Placeholder Text blah blah.</description>
<image>sampleimage1.jpg</image>
</auction>
<auction>
<time>7:00</time>
<title>Example Auction 2</title>
<description>Placeholder text blbalblbladflknsdf</description>
<image>imagepath11.jpg</image>
</auction>
<auction>
<time>8:00</time>
<title>Sample Auction 3</title>
<description>Placeholder text asflkamdflkmasdfm</description>
<image>imagepath12.jpg</image>
</auction>
<auction>
<time>9:00</time>
<title>Sample Auction 4</title>
<description>Placeholder text blabjadsflkm afdlkmasf afmlksf mmasdflkm</description>
<image>imagepath12.jpg</image>
</auction>
任何帮助将不胜感激。
根据以下评论,我现在有了这个:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
flash.system.Security.allowDomain('*');
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:Number = my_date.getUTCHours() + (my_date.getUTCMinutes() / 60);
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
for(var i:uint = 0; i < xmlData.auction.length(); i++)
{
var started:XMLList = xmlData.auction.(parseTime(time) >= currentHour);
for each (var item:XML in started)
{
auctionName_txt.text = xmlData.auction[i].title;
auctionDesc_txt.text = xmlData.auction[i].description;
}
}
}
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
return hour + minute / 60;
}
无论我将时钟设置为什么时间,都会返回最后一个节点。我可以说这很接近,但它似乎并不局限于节点时间,而只是循环到最后。
这里是最新的代码 - 现在不生成任何数据,但不会抛出任何编译器错误。时钟调整到什么并不重要:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
flash.system.Security.allowDomain('*');
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:String = my_date.getUTCHours() +":" + my_date.getUTCMinutes();
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = new XML(loader_ul.data);
var started:XMLList = xmlData.auction.(parseTime(time) < currentHour);
for each (var item:XML in started)
{
auctionName_txt.text = item.title;
auctionDesc_txt.text = item.description;
}
}
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
return hour + minute / 60;
}
答案 0 :(得分:0)
var my_date:Date = new Date();
var currentHour:String = my_date.getUTCHours() +":" + my_date.getUTCMinutes();
- 您将以HH:MM格式获得UTC时间。
trace(xml.auction.(time == currentHour))
将跟踪节点,其中包含“time”标记值== currentHour
但在你的情况下,我们正在比较时间。 确保解析xml时间值并将其强制转换为数字。
您的 fileLoaded 函数应如下所示:
function fileLoaded(e:Event):void
{
xmlData = new XML(loader_ul.data);
var started:XMLList = xmlData.auction.(parseTime(time) < currentHour);
for each (var item:XML in started)
{
auctionName_txt.text = item.title;
auctionDesc_txt.text = item.description;
}
}
同时定义 parseTime 函数,如果您的XML时间是UTC :
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
return hour + minute / 60;
}
或 XML时间是当地时间:
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
var date:Date = new Date();
date.setHours(hour);
date.setMinutes(minute);
return date.getUTCHours() + date.getUTCMinutes() / 60
}