E4X:在 XMLList 中插入节点以查找缺失的日期?

发布于 2024-08-02 08:49:15 字数 1064 浏览 8 评论 0原文

如果我有一个像这样的 XML 对象:

    <a>
    <u date="2009-04-10" value="543"/>
    <u date="2009-04-11" value="234"/>
    <u date="2009-04-13" value="321"/>
    <u date="2009-04-14" value="66"/>
    <u date="2009-04-16" value="234"/>

    <t date="2009-04-01" value="43"/>
    <t date="2009-04-02" value="67"/>
    <t date="2009-04-03" value="432"/>
    <t date="2009-04-08" value="123"/>
    <t date="2009-04-09" value="65"/>

    <l date="2009-04-01" value="12"/>
    <l date="2009-04-02" value="76"/>
    <l date="2009-04-03" value="123"/>
    <l date="2009-04-04" value="6543"/>
    <l date="2009-04-05" value="123"/>
    <l date="2009-04-06" value="65"/>
    <l date="2009-04-15" value="234"/>
    <l date="2009-04-16" value="65"/>
</a>

此 XML 对象中有 3 个 XMLList。如果您注意到,日期之间有间隙。有没有办法将缺失的日期添加到每个 XMLList 中?值为 0。

另外,我不想在每个 XMLList 中的第一个和最后一个节点之前或之后添加任何日期...我只想填充每个节点之间缺少的日期。

我怎样才能做到这一点?

谢谢!!!

if I have an XML object like this:

    <a>
    <u date="2009-04-10" value="543"/>
    <u date="2009-04-11" value="234"/>
    <u date="2009-04-13" value="321"/>
    <u date="2009-04-14" value="66"/>
    <u date="2009-04-16" value="234"/>

    <t date="2009-04-01" value="43"/>
    <t date="2009-04-02" value="67"/>
    <t date="2009-04-03" value="432"/>
    <t date="2009-04-08" value="123"/>
    <t date="2009-04-09" value="65"/>

    <l date="2009-04-01" value="12"/>
    <l date="2009-04-02" value="76"/>
    <l date="2009-04-03" value="123"/>
    <l date="2009-04-04" value="6543"/>
    <l date="2009-04-05" value="123"/>
    <l date="2009-04-06" value="65"/>
    <l date="2009-04-15" value="234"/>
    <l date="2009-04-16" value="65"/>
</a>

There is 3 XMLLists in this XML object. If you notice, the dates have gaps in them. Is there a way to add the missing dates to each XMLList? With a value of 0.

Also I do not want to add any dates before or after the first and last node in each XMLList...i just want to fill in the missing dates between each node.

How can I do that?

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-08-09 08:49:15

不幸的是,这并不容易。 Actionscript 中没有 timedelta 类(如 Python)。这使得查找日期之间的距离变得有点痛苦。如果您可以保证日期始终在同一个月内(例如给定月份内的 1-31),那也不算太糟糕。然后你可以使用类似的东西:

package
{
import flash.display.Sprite;

public class TestXML extends Sprite
{
public function TestXML()
{
    var xml:XML = 
    <a>
        <u date="2009-04-10" value="543"/>
        <u date="2009-04-11" value="234"/>
        <u date="2009-04-13" value="321"/>
        <u date="2009-04-14" value="66"/>
        <u date="2009-04-16" value="234"/>

        <t date="2009-04-01" value="43"/>
        <t date="2009-04-02" value="67"/>
        <t date="2009-04-03" value="432"/>
        <t date="2009-04-08" value="123"/>
        <t date="2009-04-09" value="65"/>

        <l date="2009-04-01" value="12"/>
        <l date="2009-04-02" value="76"/>
        <l date="2009-04-03" value="123"/>
        <l date="2009-04-04" value="6543"/>
        <l date="2009-04-05" value="123"/>
        <l date="2009-04-06" value="65"/>
        <l date="2009-04-15" value="234"/>
        <l date="2009-04-16" value="65"/>
    </a>; // / // <-- need this for stack overflow parse bug :(

    fillBlanks(xml, xml..u);
    fillBlanks(xml, xml..t);
    fillBlanks(xml, xml..l);
}

private function fillBlanks(rootNode:XML, list:XMLList):void
{   
    var dateString:String;
    var matches:Array;
    var currentDate:Date;
    var lastDate:Date;
    for each(var node:XML in list)
    {
        dateString = [email protected]();
        matches = dateString.match(/(\d+)\-(\d+)\-(\d+)/);
        currentDate = new Date(matches[1], matches[2], matches[3]);

        while(lastDate && (currentDate.date - lastDate.date) != 1)
        {
            rootNode.insertChildBefore(node, new XML(
                "<" + node.name() + " date=\"" + 
                lastDate.fullYear + 
                "-" + 
                lastDate.month + 
                "-" + 
                (lastDate.date + 1) + 
                "\" value=\"0\" />"));

            lastDate = new Date(lastDate.fullYear, lastDate.month, lastDate.date + 1);
        }

        lastDate = currentDate;
    }
}
}
}

这不会做一些花哨的事情,比如在月份或小于 10 的日期之前填充“0”。如果你的范围跨越月份障碍,它也不会处理。第一个问题很容易解决,所以我把它留给你。第二个根本不是很容易(特别是如果日期也跨越年份界限),我再次将其留给您。

It isn't that easy unfortunately. There is not timedelta class (like Python) in Actionscript. This makes finding the distance between dates a bit of a pain. It isn't too bad if you can guarantee that the dates will always be within the same month (e.g. 1-31 within a given month). Then you can use something like:

package
{
import flash.display.Sprite;

public class TestXML extends Sprite
{
public function TestXML()
{
    var xml:XML = 
    <a>
        <u date="2009-04-10" value="543"/>
        <u date="2009-04-11" value="234"/>
        <u date="2009-04-13" value="321"/>
        <u date="2009-04-14" value="66"/>
        <u date="2009-04-16" value="234"/>

        <t date="2009-04-01" value="43"/>
        <t date="2009-04-02" value="67"/>
        <t date="2009-04-03" value="432"/>
        <t date="2009-04-08" value="123"/>
        <t date="2009-04-09" value="65"/>

        <l date="2009-04-01" value="12"/>
        <l date="2009-04-02" value="76"/>
        <l date="2009-04-03" value="123"/>
        <l date="2009-04-04" value="6543"/>
        <l date="2009-04-05" value="123"/>
        <l date="2009-04-06" value="65"/>
        <l date="2009-04-15" value="234"/>
        <l date="2009-04-16" value="65"/>
    </a>; // / // <-- need this for stack overflow parse bug :(

    fillBlanks(xml, xml..u);
    fillBlanks(xml, xml..t);
    fillBlanks(xml, xml..l);
}

private function fillBlanks(rootNode:XML, list:XMLList):void
{   
    var dateString:String;
    var matches:Array;
    var currentDate:Date;
    var lastDate:Date;
    for each(var node:XML in list)
    {
        dateString = [email protected]();
        matches = dateString.match(/(\d+)\-(\d+)\-(\d+)/);
        currentDate = new Date(matches[1], matches[2], matches[3]);

        while(lastDate && (currentDate.date - lastDate.date) != 1)
        {
            rootNode.insertChildBefore(node, new XML(
                "<" + node.name() + " date=\"" + 
                lastDate.fullYear + 
                "-" + 
                lastDate.month + 
                "-" + 
                (lastDate.date + 1) + 
                "\" value=\"0\" />"));

            lastDate = new Date(lastDate.fullYear, lastDate.month, lastDate.date + 1);
        }

        lastDate = currentDate;
    }
}
}
}

This doesn't do fancy stuff like pad "0" before months or dates that are less than 10. It also will not handle if your ranges cross month barriers. The first is really easy to fix so I leave it to you. The second is not very easy at all (especially if the dates cross year boundaries as well) and again I'll leave it to you.

メ斷腸人バ 2024-08-09 08:49:15

如果您有 XML.ignoreWhitespace=true,那么您的 XML 将忽略这些间隙,并且 a.children() 将为您提供所有真实子节点的列表...如果为 false,您可以执行以下操作:

var myList:XMLList = xml.u + xml.t + xml.l;

填充间隙对于空日期,请确保ignoreWhitespace为假,这样的事情应该可以工作:

var myList:XMLList = xml.u + <x date="0" value="0"/> + xml.t + <x date="0" value="0"/> + xml.l;

If you have XML.ignoreWhitespace=true, then your XML will ignore those gaps, and a.children() will give you a list of all the real children nodes... If it's false, you can do this:

var myList:XMLList = xml.u + xml.t + xml.l;

To fill the gaps with null dates make sure ignoreWhitespace is false, and something like this should work:

var myList:XMLList = xml.u + <x date="0" value="0"/> + xml.t + <x date="0" value="0"/> + xml.l;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文