By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Converting string into a numeric date value

P: 16
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm trying to sort them by the date modified field.

So what's the problem, right?

Well, obviously Sep 14, 2008 is before Nov 14, 2008, but when doing a sort process, ActionScript will come back with Nov coming before Sep, because N comes before S in any sort.

So I figured that I would convert the string to a number and then later specify that this is a numeric sort.

Here's the code that traverses through the XML and changes the string to numbers:

Expand|Select|Wrap|Line Numbers
  1.             public function createDate(s:String):String
  2.             {
  3.                 var retStr:String = '';
  4.                 var str2xml:XML = new XML(s);
  5.                 var xml2list:XMLList = new XMLList(str2xml);
  6.                 var list2col:XMLListCollection = new XMLListCollection(xml2list);
  7.  
  8.                 var mods:XMLList = list2col.children().child("modified");
  9.  
  10.                 var mod:String = list2col.children().child("modified").text();
  11.                 for each(var h:XML in mods)
  12.                 {
  13.                     var xml2str:String = h;
  14.                     trace("list2col"+list2col.children().child("modified").text());
  15.                     switch(xml2str.substr(0,4))
  16.                     {
  17.                         case "Jan ":
  18.                         xml2str=xml2str.replace("Jan ","01");
  19.                         xml2str=xml2str.replace(" ","");
  20.                         xml2str=xml2str.replace(",","");
  21.                         xml2str=xml2str.replace(" ","");
  22.                         xml2str=xml2str.substr(0,8);
  23.                         retStr = xml2str;
  24.                         break;
  25.                         case "Feb ":
  26.                         xml2str=xml2str.replace("Feb ","01");
  27.                         xml2str=xml2str.replace(" ","");
  28.                         xml2str=xml2str.replace(",","");
  29.                         xml2str=xml2str.replace(" ","");
  30.                         xml2str=xml2str.substr(0,8);
  31.                         retStr = xml2str;
  32.                         break;
  33.                         case "Mar ":
  34.                         xml2str=xml2str.replace("Mar ","01");
  35.                         xml2str=xml2str.replace(" ","");
  36.                         xml2str=xml2str.replace(",","");
  37.                         xml2str=xml2str.replace(" ","");
  38.                         xml2str=xml2str.substr(0,8);
  39.                         retStr = xml2str;
  40.                         break;
  41.                         case "Apr ":
  42.                         xml2str=xml2str.replace("Apr ","01");
  43.                         xml2str=xml2str.replace(" ","");
  44.                         xml2str=xml2str.replace(",","");
  45.                         xml2str=xml2str.replace(" ","");
  46.                         xml2str=xml2str.substr(0,8);
  47.                         retStr = xml2str;
  48.                         break;
  49.                         case "May ":
  50.                         xml2str=xml2str.replace("May ","01");
  51.                         xml2str=xml2str.replace(" ","");
  52.                         xml2str=xml2str.replace(",","");
  53.                         xml2str=xml2str.replace(" ","");
  54.                         xml2str=xml2str.substr(0,8);
  55.                         retStr = xml2str;
  56.                         break;
  57.                         case "Jun ":
  58.                         xml2str=xml2str.replace("Jun ","01");
  59.                         xml2str=xml2str.replace(" ","");
  60.                         xml2str=xml2str.replace(",","");
  61.                         xml2str=xml2str.replace(" ","");
  62.                         xml2str=xml2str.substr(0,8);
  63.                         retStr = xml2str;
  64.                         break;
  65.                         case "Jul ":
  66.                         xml2str=xml2str.replace("Jul ","01");
  67.                         xml2str=xml2str.replace(" ","");
  68.                         xml2str=xml2str.replace(",","");
  69.                         xml2str=xml2str.replace(" ","");
  70.                         xml2str=xml2str.substr(0,8);
  71.                         retStr = xml2str;
  72.                         break;
  73.                         case "Aug ":
  74.                         xml2str=xml2str.replace("Aug ","01");
  75.                         xml2str=xml2str.replace(" ","");
  76.                         xml2str=xml2str.replace(",","");
  77.                         xml2str=xml2str.replace(" ","");
  78.                         xml2str=xml2str.substr(0,8);
  79.                         retStr = xml2str;
  80.                         break;
  81.                         case "Sep ":
  82.                         xml2str=xml2str.replace("Sep ","01");
  83.                         xml2str=xml2str.replace(" ","");
  84.                         xml2str=xml2str.replace(",","");
  85.                         xml2str=xml2str.replace(" ","");
  86.                         xml2str=xml2str.substr(0,8);
  87.                         retStr = xml2str;
  88.                         break;
  89.                         case "Oct ":
  90.                         xml2str=xml2str.replace("Oct ","01");
  91.                         xml2str=xml2str.replace(" ","");
  92.                         xml2str=xml2str.replace(",","");
  93.                         xml2str=xml2str.replace(" ","");
  94.                         xml2str=xml2str.substr(0,8);
  95.                         retStr = xml2str;
  96.                         break;
  97.                         case "Nov ":
  98.                         xml2str=xml2str.replace("Nov ","01");
  99.                         xml2str=xml2str.replace(" ","");
  100.                         xml2str=xml2str.replace(",","");
  101.                         xml2str=xml2str.replace(" ","");
  102.                         xml2str=xml2str.substr(0,8);
  103.                         retStr = xml2str;
  104.                         break;
  105.                         case "Dec ":
  106.                         xml2str=xml2str.replace("Dec ","01");
  107.                         xml2str=xml2str.replace(" ","");
  108.                         xml2str=xml2str.replace(",","");
  109.                         xml2str=xml2str.replace(" ","");
  110.                         xml2str=xml2str.substr(0,8);
  111.                         retStr = xml2str;
  112.                         break;
  113.                         default:
  114.                         // do nothing
  115.                     }
  116.                     trace("retStr"+retStr);
  117.                 }
  118.                 return retStr;
  119.             }
  120.  
No problem, right? This definitely works, but that is not where my issue lies.

How do I re-insert the converted values in the exact same place as where it was located when I received the xml?

Or do I have to grab every object and reassemble it?

I'm hoping there is an easier way and, as usual, your help is appreciated and thanx n advance
Nov 15 '08 #1
Share this Question
Share on Google+
3 Replies


joedeene
100+
P: 583
I'm not an expert with integrating XML and Flash, but could you like create a new instance of the XML file and copy all of the data from the XML file you opened into that instance. Therefore, whenever you change anything, you'd change the second instance, leaving the originally XML file, unchanged?

If worse comes to worse, you might have to "reassemble" the XML file...

joedeene
Nov 15 '08 #2

P: 16
Thanx 4 d idea of treating it as one object/instance.

After making my replaces, then it was time to return the whole xml object:
Expand|Select|Wrap|Line Numbers
  1.             public function createDateModified(s:String):XML
  2.             {
  3.                 var i:int = 0;
  4.                 var retStr:String = '';
  5.                 var str2xml:XML = new XML(s);
  6.                 var xml2list:XMLList = new XMLList(str2xml);
  7.                 var list2col:XMLListCollection = new XMLListCollection(xml2list);
  8.  
  9.                 var mods:XMLList = list2col.children().child("modified");
  10.  
  11.                 var mod:String = list2col.children().child("modified").text();
  12.                 for each(var h:XML in str2xml.children().child("modified"))
  13.                 {
  14.                     var someStr:String = str2xml.children().modified[i].text();
  15.                     switch(someStr.substr(0,4))
  16.                     {
  17.                         case "Jan ":
  18.                         if(someStr.substr(6,1) != ",")
  19.                         {
  20.                             someStr=someStr.replace("Jan ","010");                            
  21.                         }
  22.                         else
  23.                         {
  24.                             someStr=someStr.replace("Jan ","01");
  25.                         }
  26.                         someStr=someStr.replace(" ","");
  27.                         someStr=someStr.replace(",","");
  28.                         someStr=someStr.replace(" ","");
  29.                         someStr=someStr.substr(0,8);
  30.                         str2xml.children().modified[i] = someStr;
  31.                         i++;
  32.                         break;
  33.                         case "Feb ":
  34.                         if(someStr.substr(6,1) != ",")
  35.                         {
  36.                             someStr=someStr.replace("Feb ","020");                            
  37.                         }
  38.                         else
  39.                         {
  40.                             someStr=someStr.replace("Feb ","02");
  41.                         }
  42.                         someStr=someStr.replace(" ","");
  43.                         someStr=someStr.replace(",","");
  44.                         someStr=someStr.replace(" ","");
  45.                         someStr=someStr.substr(0,8);
  46.                         str2xml.children().modified[i] = someStr;
  47.                         i++;
  48.                         break;
  49.                         case "Mar ":
  50.                         if(someStr.substr(6,1) != ",")
  51.                         {
  52.                             someStr=someStr.replace("Mar ","030");                            
  53.                         }
  54.                         else
  55.                         {
  56.                             someStr=someStr.replace("Mar ","03");
  57.                         }
  58.                         someStr=someStr.replace(" ","");
  59.                         someStr=someStr.replace(",","");
  60.                         someStr=someStr.replace(" ","");
  61.                         someStr=someStr.substr(0,8);
  62.                         str2xml.children().modified[i] = someStr;
  63.                         i++;
  64.                         break;
  65.                         case "Apr ":
  66.                         if(someStr.substr(6,1) != ",")
  67.                         {
  68.                             someStr=someStr.replace("Apr ","040");                            
  69.                         }
  70.                         else
  71.                         {
  72.                             someStr=someStr.replace("Apr ","04");
  73.                         }
  74.                         someStr=someStr.replace(" ","");
  75.                         someStr=someStr.replace(",","");
  76.                         someStr=someStr.replace(" ","");
  77.                         someStr=someStr.substr(0,8);
  78.                         str2xml.children().modified[i] = someStr;
  79.                         i++;
  80.                         break;
  81.                         case "May ":
  82.                         if(someStr.substr(6,1) != ",")
  83.                         {
  84.                             someStr=someStr.replace("May ","050");                            
  85.                         }
  86.                         else
  87.                         {
  88.                             someStr=someStr.replace("May ","05");
  89.                         }
  90.                         someStr=someStr.replace(" ","");
  91.                         someStr=someStr.replace(",","");
  92.                         someStr=someStr.replace(" ","");
  93.                         someStr=someStr.substr(0,8);
  94.                         str2xml.children().modified[i] = someStr;
  95.                         i++;
  96.                         break;
  97.                         case "Jun ":
  98.                         if(someStr.substr(6,1) != ",")
  99.                         {
  100.                             someStr=someStr.replace("Jun ","060");                            
  101.                         }
  102.                         else
  103.                         {
  104.                             someStr=someStr.replace("Jun ","06");
  105.                         }
  106.                         someStr=someStr.replace(" ","");
  107.                         someStr=someStr.replace(",","");
  108.                         someStr=someStr.replace(" ","");
  109.                         someStr=someStr.substr(0,8);
  110.                         str2xml.children().modified[i] = someStr;
  111.                         i++;
  112.                         break;
  113.                         case "Jul ":
  114.                         if(someStr.substr(6,1) != ",")
  115.                         {
  116.                             someStr=someStr.replace("Jul ","070");                            
  117.                         }
  118.                         else
  119.                         {
  120.                             someStr=someStr.replace("Jul ","07");
  121.                         }
  122.                         someStr=someStr.replace(" ","");
  123.                         someStr=someStr.replace(",","");
  124.                         someStr=someStr.replace(" ","");
  125.                         someStr=someStr.substr(0,8);
  126.                         str2xml.children().modified[i] = someStr;
  127.                         i++;
  128.                         break;
  129.                         case "Aug ":
  130.                         if(someStr.substr(6,1) != ",")
  131.                         {
  132.                             someStr=someStr.replace("Aug ","080");                            
  133.                         }
  134.                         else
  135.                         {
  136.                             someStr=someStr.replace("Aug ","08");
  137.                         }
  138.                         someStr=someStr.replace(" ","");
  139.                         someStr=someStr.replace(",","");
  140.                         someStr=someStr.replace(" ","");
  141.                         someStr=someStr.substr(0,8);
  142.                         str2xml.children().modified[i] = someStr;
  143.                         i++;
  144.                         break;
  145.                         case "Sep ":
  146.                         if(someStr.substr(6,1) != ",")
  147.                         {
  148.                             someStr=someStr.replace("Sep ","090");                            
  149.                         }
  150.                         else
  151.                         {
  152.                             someStr=someStr.replace("Sep ","09");
  153.                         }
  154.                         someStr=someStr.replace(" ","");
  155.                         someStr=someStr.replace(",","");
  156.                         someStr=someStr.replace(" ","");
  157.                         someStr=someStr.substr(0,8);
  158.                         str2xml.children().modified[i] = someStr;
  159.                         i++;
  160.                         break;
  161.                         case "Oct ":
  162.                         if(someStr.substr(6,1) != ",")
  163.                         {
  164.                             someStr=someStr.replace("Oct ","100");                            
  165.                         }
  166.                         else
  167.                         {
  168.                             someStr=someStr.replace("Oct ","10");
  169.                         }
  170.                         someStr=someStr.replace(" ","");
  171.                         someStr=someStr.replace(",","");
  172.                         someStr=someStr.replace(" ","");
  173.                         someStr=someStr.substr(0,8);
  174.                         str2xml.children().modified[i] = someStr;
  175.                         i++;
  176.                         break;
  177.                         case "Nov ":
  178.                         trace("comma"+someStr.substr(6,1));
  179.                         if(someStr.substr(6,1) != ",")
  180.                         {
  181.                             someStr=someStr.replace("Nov ","110");                            
  182.                         }
  183.                         else
  184.                         {
  185.                             someStr=someStr.replace("Nov ","11");
  186.                         }
  187.                         someStr=someStr.replace(" ","");
  188.                         someStr=someStr.replace(",","");
  189.                         someStr=someStr.replace(" ","");
  190.                         someStr=someStr.substr(0,8);
  191.                         str2xml.children().modified[i] = someStr;
  192.                         i++;
  193.                         break;
  194.                         case "Dec ":
  195.                         if(someStr.substr(6,1) != ",")
  196.                         {
  197.                             someStr=someStr.replace("Dec ","120");                            
  198.                         }
  199.                         else
  200.                         {
  201.                             someStr=someStr.replace("Dec ","12");
  202.                         }
  203.                         someStr=someStr.replace(" ","");
  204.                         someStr=someStr.replace(",","");
  205.                         someStr=someStr.replace(" ","");
  206.                         someStr=someStr.substr(0,8);
  207.                         str2xml.children().modified[i] = someStr;
  208.                         i++;
  209.                         break;
  210.                         default:
  211.                         // do nothing
  212.                     }
  213.                     //trace("retStr"+retStr);
  214.                 }
  215.                 return str2xml;
  216.             }
  217.  
Nov 16 '08 #3

joedeene
100+
P: 583
So, you fixed the problem? Because that was a lot of code, haha. I just skimmed it.

joedeene
Nov 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.