views:

841

answers:

2

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

A: 

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;
Cay
I am sorry..I did not mean gaps as in white space...I meant gaps between dates like how < u > goes from 2009-04-14 to 2009-04-16 I want to add a node with the date 2009-04-15 with value of 0. So there is no gaps in the dates. Get it?
John Isaacks
A: 

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.

James Fassett