获取时间由使用XML AS3 [英] Get the time from XML using AS3

查看:137
本文介绍了获取时间由使用XML AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将显示基于时间的各种拍卖的项目的XML文件。我想使用AS3基于所小时集合中的XML文件的字段GETUTCDATE然后显示适当的XML节点。我是pretty的很多中间用AS3 + XML的,所以我可以使用一些帮助。我已经得到的一切设置,但东西是不完全正确。

AS3 code:

 进口flash.net.URLRequest;
进口flash.net.URLLoader;
进口对象类型:flash.events.Event;
进口flash.text.TextFormat用于;

VAR XMLDATA:XML =新的XML();
VAR theURL_ur:的URLRequest =新的URLRequest(sample_auction.xml);
VAR loader_ul:的URLLoader =新的URLLoader(theURL_ur);
loader_ul.addEventListener(完全,fileLoaded);


VAR my_date:日期=新的日期();
VAR currentHour的:数= my_date.getUTCHours()+(my_date.getUTCMinutes()/ 60);
跟踪(currentHour的);


功能fileLoaded(五:事件):无效
{
XMLDATA = XML(loader_ul.data);
对于(VAR我:UINT = 0; I< xmlData.auction.length();我++)
{
    如果(xmlData.auction [I]。时间< currentHour的)
    {
    auctionName_txt.text = xmlData.auction [I] .title伪;
    auctionDesc_txt.text = xmlData.auction [I] .DESCRIPTION;
    }
}

}
 

我需要从XML中的不同时间宣布比如做一个VAR每次什么呢?我怎样才能得到它,因此AS3知道,时间节点是一个实际的时间呢?下面是一些示例XML code。 (也我不打电话的照片呢,我只是试图让这件事情,首先阅读相应的节点。)这不是抛出编译错误,但它也不能确定做呢。

中的XML code:

 < auctionlist>
<拍卖>
&其中;时间> 6:00其中; /时间>
<标题物实施例拍卖1< /标题>
<描述>占位符文本等等等等< /描述>
<图像> sampleimage1.jpg< /图片>
< /拍卖>


<拍卖>
<时间> 7:00< /时间>
<标题物实施例拍卖2'; /标题>
<描述>占位符文本blbalblbladflknsdf< /描述>
<图像> imagepath11.jpg< /图片>
< /拍卖>


<拍卖>
&其中;时间> 8:00&其中; /时间>
<冠军>样本拍卖3'; /标题>
<描述>占位符文本asflkamdflkmasdfm< /描述>
<图像> imagepath12.jpg< /图片>
< /拍卖>


<拍卖>
<时间> 9:00< /时间>
<冠军>样本拍卖4℃/标题>
<描述>占位符文本blabjadsflkm afdlkmasf afmlksf mmasdflkm< /描述>
<图像> imagepath12.jpg< /图片>
< /拍卖>
 

任何帮助将大大AP preciated。

确定基于下面的评论,我现在有这样的:

 进口flash.net.URLRequest;
进口flash.net.URLLoader;
进口对象类型:flash.events.Event;
进口flash.text.TextFormat用于;

flash.system.Security.allowDomain('*');

VAR XMLDATA:XML =新的XML();
VAR theURL_ur:的URLRequest =新的URLRequest(sample_auction.xml);
VAR loader_ul:的URLLoader =新的URLLoader(theURL_ur);
loader_ul.addEventListener(完全,fileLoaded);

VAR my_date:日期=新的日期();
VAR currentHour的:数= my_date.getUTCHours()+(my_date.getUTCMinutes()/ 60);
跟踪(currentHour的);

功能fileLoaded(五:事件):无效
{
XMLDATA = XML(loader_ul.data);
对于(VAR我:UINT = 0; I< xmlData.auction.length();我++)
{
VAR开始:返回XMLList = xmlData.auction(parseTime(时间)GT; = currentHour的);
每个(VAR项目:XML的开始)
{
    auctionName_txt.text = xmlData.auction [I] .title伪;
    auctionDesc_txt.text = xmlData.auction [I] .DESCRIPTION;
}
}
}

功能parseTime(时间:字符串):数{
VAR小时:INT = time.split(:)[0];
VAR分钟:INT = time.split(:)[1];
返回小时+分钟/ 60;
}
 

这将返回的最后一个节点无论什么时候,我把我的时钟。我可以告诉大家,这是接近,但它似乎贯穿始终不限制到节点的时间,但只是循环。

修订

确定这里是最新的code - 现在不产生数据,但不引发编译器错误。什么都无所谓的时钟调整为:

 进口flash.net.URLRequest;
进口flash.net.URLLoader;
进口对象类型:flash.events.Event;
进口flash.text.TextFormat用于;

flash.system.Security.allowDomain('*');

VAR XMLDATA:XML =新的XML();
VAR theURL_ur:的URLRequest =新的URLRequest(sample_auction.xml);
VAR loader_ul:的URLLoader =新的URLLoader(theURL_ur);
loader_ul.addEventListener(完全,fileLoaded);

VAR my_date:日期=新的日期();
VAR currentHour的:字符串= my_date.getUTCHours()+:+ my_date.getUTCMinutes();
跟踪(currentHour的);



功能fileLoaded(五:事件):无效
{
XMLDATA =新的XML(loader_ul.data);
VAR开始:返回XMLList = xmlData.auction(parseTime(时间)LT; currentHour的);
每个(VAR项目:XML的开始)
{
    auctionName_txt.text = item.title;
auctionDesc_txt.text = item.description;
}
}

功能parseTime(时间:字符串):数{
VAR小时:INT = time.split(:)[0];
VAR分钟:INT = time.split(:)[1];
返回小时+分钟/ 60;
}
 

解决方案

  VAR my_date:日期=新的日期();
VAR currentHour的:字符串= my_date.getUTCHours()+:+ my_date.getUTCMinutes();
 

- 您将获得UTC时间HH:MM格式

 跟踪(xml.auction。(时间== currentHour的))
 

将跟踪节点,其中有时间变量值== currentHour的


但在你的情况,我们比较的时间。 请确保您解析XML时间值,并投它数编号方式。

您的 fileLoaded 的功能应该是这样的:

 函数fileLoaded(五:事件):无效
{
    XMLDATA =新的XML(loader_ul.data);
    VAR开始:返回XMLList = xmlData.auction(parseTime(时间)LT; currentHour的);
    每个(VAR项目:XML的开始)
    {
        auctionName_txt.text = item.title;
        auctionDesc_txt.text = item.description;
    }
}
 

另外定义的 parseTime 的功能,如果你的XML时间为UTC

 函数parseTime(时间:字符串):数{
    VAR小时:INT = time.split(:)[0];
    VAR分钟:INT = time.split(:)[1];
    返回小时+分钟/ 60;
}
 

如果您的 XML时间当地时间

 函数parseTime(时间:字符串):数{
    VAR小时:INT = time.split(:)[0];
    VAR分钟:INT = time.split(:)[1];
    VAR日期:日期=新的日期();
    date.setHours(小时);
    date.setMinutes(分钟);
    返回date.getUTCHours()+ date.getUTCMinutes()/ 60
}
 

I have an XML file of various auction items that will display based on the time. I want to use AS3 to getUTCDate based on the hour set in the field of the XML file and then display the appropriate XML node. I'm pretty much intermediate with AS3+XML so I could use some help. I've got everything setup but something isn't quite right.

AS3 Code:

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;
    }
}

}

do I need to declare the various times from xml such as doing a var on each time or something? how can I get it so the AS3 knows that the time node is an actual time? Here is some sample XML code. (also I'm not calling the image yet I'm just trying to get this thing to read the appropriate nodes first.) It's not throwing a complier error but it's also not sure what to do yet.

The XML Code:

<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>

any help would be greatly appreciated.

Ok based on the comments below, I now have this:

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;
}

This returns the last node no matter what time I set my clock to. I can tell this is close but it seems to not restrict to the node time but just loop through to the end.

UPDATED

Ok here is latest code - now produces no data but throws no compiler errors. Doesn't matter what the clock is adjusted to:

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;
}

解决方案

var my_date:Date = new Date();
var currentHour:String = my_date.getUTCHours() +":" + my_date.getUTCMinutes();

— You will get UTC time in HH:MM format.

trace(xml.auction.(time == currentHour))

Will trace node, which has "time" tag value == currentHour


But in your case we are comparing time. Make sure you parse xml time value and cast it to Number right way.

Your fileLoaded function should look like this:

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; 
    }       
}

Also define parseTime function, if your XML time is UTC:

function parseTime(time:String):Number {
    var hour:int = time.split(":")[0];
    var minute:int = time.split(":")[1];
    return hour + minute / 60;
}

or if your XML time is your local time:

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
}

这篇关于获取时间由使用XML AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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