-
AS2 Date and time formatting
Hi, I'm importing text into a datagrid which works fine (thanks to a_modified_dog) I now need to convert two of the nodes from the xml into a format that is usable. One is in milliseconds and the other is in date format.
If the node reads:
<n_StartDateSort>20090531</n_StartDateSort> I need it to read 31.05.2009
<c_Result1>119615000</c_Result1> I need it to read 33:13.35
My files are attached (Flash version 8).
I would really appreciate some help as I don't seem to be getting anywhere.
MY XML LOOKS LIKE THIS
<Sports>
<DocInfo>
<Timezone>CET</Timezone>
<ContentType>Results</ContentType>
<Language>en</Language>
</DocInfo>
<Ranking>
<Header>
<n_CompetitionID>131</n_CompetitionID>
<n_CompetitionCode>8</n_CompetitionCode>
<n_CompetitionSet>0</n_CompetitionSet>
<n_CompetitionLevel>0</n_CompetitionLevel>
</Header>
<Detail>
<n_StartDateSort>20090531</n_StartDateSort>
<c_Athlete>Denis Martin</c_Athlete>
<c_Result1>119615000</c_Result1>
</Detail>
<Detail>
<n_StartDateSort>20090530</n_StartDateSort>
<c_Athlete>Martin Johns</c_Athlete>
<c_Result1>119515000</c_Result1>
</Detail>
<Detail>
<n_StartDateSort>20090529</n_StartDateSort>
<c_Athlete>David Lee</c_Athlete>
<c_Result1>119415000</c_Result1>
</Detail>
</Ranking>
</Sports>
MY CODE LOOKS LIKE THIS
var my1_XML = new XML();
my1_XML.ignoreWhite = true;
myDg_dg.removeAll();
myDg_dg.removeAllColumns();
my1_XML.load("data.xml");
my1_XML.onLoad = loadmy1XML;
header = 1;
function loadmy1XML() {
wholeXML = this.firstChild;
rankings = wholeXML.childNodes[1];
nodeLength = rankings.childNodes.length;
for (var i:Number = 1; i<=nodeLength-1; i++) {
len = rankings.childNodes[i].childNodes.length;
for (var j:Number = 0; j!=len; j++) {
nName = rankings.childNodes[i].childNodes[j].nodeName; trace(nName);
// n_StartDateSort
if(nName == "n_StartDateSort"){
n_StartDateSort = rankings.childNodes[i].childNodes[j].childNodes; trace("***"+n_StartDateSort);
}
// c_Athlete
if(nName == "c_Athlete"){
c_Athlete = rankings.childNodes[i].childNodes[j].childNodes; trace("***"+c_Athlete);
}
// c_Result1
if(nName == "c_Result1"){
c_Result1 = rankings.childNodes[i].childNodes[j].childNodes; trace("***"+c_Result1);
}
} // end for j
if(header == 1){
myDg_dg.addItem({Start:n_StartDateSort, Athlete:c_Athlete, Time:c_Result1});
}
var myDP_array:Array = new Array({Start:n_StartDateSort, Athlete:c_Athlete, Time:c_Result1});
} // end for i
}; // end function
myDg_dg.addEventListener("headerRelease",listener_ obj);
Last edited by Embussy; 10-30-2009 at 01:49 AM.
-
FK'n_dog
for Date, make use of substr to strip out the date format
for Result, do some Math with the milliseconds
PHP Code:
// n_StartDateSort
if(nName == "n_StartDateSort"){
StartDate = rankings.childNodes[i].childNodes[j].childNodes;
d_day = StartDate.toString().substr(6,2);
d_mon = StartDate.toString().substr(4,2);
d_yr = StartDate.toString().substr(0,4);
n_StartDateSort = d_day+"."+d_mon+"."+d_yr; trace("***"+n_StartDateSort);
}
// c_Athlete
if(nName == "c_Athlete"){
c_Athlete = rankings.childNodes[i].childNodes[j].childNodes; trace("***"+c_Athlete);
}
// c_Result1
if(nName == "c_Result1"){
c_Result1 = rankings.childNodes[i].childNodes[j].childNodes;
Result1 = parseInt(c_Result1);
hr = 1000*60*60;
hrs = Math.floor(Result1/hr);
remain1 = Result1-(hrs*hr);
min = 1000*60;
mins = Math.floor(remain1/min);
secs = (remain1-(mins*min))/1000;
c_Result1 = hrs+":"+mins+"."+secs; trace("***"+c_Result1);
}
} // end for j
-
Thanks a_modified_dog, that works perfectly with the Datagrid, after I got it all running I noticed a few nodes display & or '
The actual nodes in the xml look like this.......but display like this
<c_Team>Acqua & Sapone</c_Team> = Acqua & Sapone
<c_Team>Caisse d'Epargne</c_Team> = Caisse d'Epargne
I've had a good look around to try and find a solution but I can't use CDATA as I can't edit the xml and I've added the line of code - myDg_dg.embedFonts = true; but that doesn't work. Do you know how to make it work please?
Thanks again
Embussy
THE CODE I USE FOR THE DATAGRID LOOKS LIKE THIS
var myDg_dg:mx.controls.DataGrid;
myDg_dg.setStyle("fontFamily", "Arial");
myDg_dg.setStyle("headerSelectionColor", 0xFFFFFF);
myDg_dg.setStyle("headerBottomColor", 0xFFFFFF);
myDg_dg.setStyle("alternatingRowColors", ["0xF0F0F0", "0xFFFFFF"]);
myDg_dg.setStyle("rollOverColor", "0xF0F0F0");
myDg_dg.setStyle("selectionColor", "0xF0F0F0");
myDg_dg.setStyle("selectionDuration", 0);
myDg_dg.embedFonts = true;
myDg_dg.setStyle("useRollOver", false);
myDg_dg.selectable = false;
myDg_dg.setStyle("fontSize",13);
myDg_dg.rowHeight = 20;
myDg_dg.setStyle("borderStyle", "none");
myDg_dg.setStyle("vGridLines", true);
myDg_dg.setStyle("hGridLines", false);
myDg_dg.setStyle("fontColor", 0xFFFFFF);
myDg_dg.showHeaders = true;
myDg_dg.setStyle("color", "0x000000");
-
FK'n_dog
can you post a new zip file containing your fla ( Flash version 8 for me )
and your xml file, as your code above contains no reference to the
c_Team node that you are having problems with
plus - does your xml file include an encoding tag ?
like this - <?xml version="1.0" encoding="iso-8859-1" ?> or a variation of this ?
if so try adding this to your fla - system.useCodepage = true;
-
Hi a_modified_dog, I have attached a new zip file
Thank you for your help
Last edited by Embussy; 10-30-2009 at 01:49 AM.
-
FK'n_dog
add this to the top of your code. ( converts & and ' only)
ps..does the xml have a declaration ? - <?xml version="1.0" encoding="etc..?>
can you post a link to the xml source ?
PHP Code:
function entityToTxt(theSource) {
if (!entityArrays) {
HTMLentities = new Array();
TXTentities = new Array();
HTMLentities[0]="amp";
TXTentities[0]="&";
HTMLentities[1]="apos";
TXTentities[1]="\'";
entitiesLENGTH = HTMLentities.length;
entityArrays = true;
}
if(entityArrays){
a = "";
a = theSource.split("&");
a[1] = a[1].split(";");
for (i=0; i < entitiesLENGTH; i++) {
if(a[1][0] == HTMLentities[i]){
a[1][0] = TXTentities[i];
}
}
if(a[1]==undefined){
return a[0]
} else{
return a[0]+a[1][0]+a[1][1];
}
}
};
and change this line -
PHP Code:
c_Athlete = rankings.childNodes[i].childNodes[j].childNodes;
to -
PHP Code:
c_Athlete = entityToTxt(rankings.childNodes[i].childNodes[j].childNodes.toString());
Last edited by a_modified_dog; 06-15-2009 at 09:34 AM.
Reason: tidy up undefined
-
Hi a_modified_dog, Thanks very much for the code. It works perfectly for the nodes that have & and &apos but everything else that doesn't gets 'undefinedundefined' added to the end of it.
-
FK'n_dog
hmmm. strange. i edited the script above to avoid that.
(Yesterday at 02:34 PM. Reason: tidy up undefined )
if(a[1]==undefined){
return a[0]; // if no "&" is found return the athlete name
} else{
return a[0]+a[1][0]+a[1][1]; // "&" was found, return the split elements
}
can you please post the url you are receiving the xml from
so we are both using the same file. then i can test more thoroughly.
-
Hi, Sorry my fault, I tried it again in the file I sent you and it works perfectly but when I put it into the project I'm trying to make work it gives me the 'undefinedundefined' I'll have a look and see if I can fix it myself, this action script is a real brain scrambler but madly enough its actually starting to make sense finally. Is it possible to have a loader for an xml file as some of them are nearly 300kb. I have uploaded one of them to http://www.baselinetestsite.com/data.xml.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|