Hi
I have e problem, how i can create from my xml file to my csv file the rownam’s
The xml looks like this
<?xml version="1.0" encoding="UTF-8" ?>
- <meteoBase>
- <station name="Vingelz" datetime="2009-06-03 19:20:00" intervall_min="5">
<meteodata tp2m_5min_avg_cel="21.7" tp2m_1h_max_cel="22.8" tp2m_1h_max_datetime="2009-06-03 18:25:00" tp2m_today_max_cel="24.1" tp2m_today_max_datetime="2009-06-03 15:55:00" tp2m_24h_max_cel="24.1" tp2m_24h_max_datetime="2009-06-03 15:55:00" tp2m_1h_min_cel="21.7" tp2m_1h_min_datetime="2009-06-03 19:20:00" tp2m_today_min_cel="11.1" tp2m_today_min_datetime="2009-06-03 06:50:00" tp2m_24h_min_cel="11.1" tp2m_24h_min_datetime="2009-06-03 06:50:00" dwpt_5min_avg_cel="6.8" hydr_5min_avg_per="38" baro_5min_avg_hpa="1010.8" rr_sum_mm="0.0" rr_sum_1h_mm="0.0" rr_sum_today_mm="0.0" rr_sum_24h_mm="0.0" rr_sum_7d_mm="1.2" last_rr="2009-05-29 06:50:00" wnds_5min_avg_kmh="3.2" wndd_5min_avg_deg="315" wnds_5min_max_kmh="11.3" wndd_5min_max_deg="0" wnds_1h_max_kmh="17.7" wndd_1h_max_deg="0" wnds_1h_max_datetime="2009-06-03 18:45:00" wnds_today_max_kmh="22.5" wndd_today_max_deg="0" wnds_today_max_datetime="2009-06-03 15:05:00" wnds_24h_max_kmh="22.5" wndd_24h_max_deg="0" wnds_24h_max_datetime="2009-06-03 15:05:00" wntp_5min_avg_cel="21.7" hetp_5min_avg_cel="21.7" solr_5min_avg_wm2="243.0" uvrd_5min_avg_uv="-1.0" />
</station>
</meteoBase>
-
-
and my phpcode from the MagicParser like this:
<?php
$xml = '{link saved}';
$csv = "meteobase_data.csv";
require("MagicParser.php");
function myRecordHandler($record)
{
global $csv;
// unset($record["ROW"]);
// strip commas, new-line and carriage return characters from all fields
foreach($record as $key => $value)
{
$record[$key] = str_replace(","," ",$value);
$record[$key] = str_replace("\n"," ",$value);
$record[$key] = str_replace("\r"," ",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"a");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler");
fclose($csv);
// include ('wetterdatensteuerskript.php');
// sleep (3);
?>
Thank your match for the help
Oli
Hi David,
thanks for help, put is tosend working. The CSV Output show like this:
;Flueh;2009-09-07 11:15:00;5;;16.7;16.7;2009-09-07 11:15:00;16.7;2009-09-07 11:15:00;20.3;2009-09-06 17:10:00;14.5;2009-09-07 10:20:00;6.8;2009-09-07 07:00:00;6.8;2009-09-07 07:00:00;10.6;67;1021.9;0.0;0.0;0.0;0.0;32.7;2009-09-04 21:25:00;6.4;45;9.7;45;9.7;45;2009-09-07 11:15:00;9.7;45;2009-09-07 11:15:00;16.1;45;2009-09-06 14:50:00;16.7;16.7;558.0;3.4
and the PHp code
<?php
$xml = '{link saved}';
$csv = "meteobase_data.csv";
require("MagicParser.php");
function myRecordHandler($record)
{
global $csv;
// unset($record["ROW"]);
// strip commas, new-line and carriage return characters from all fields
foreach($record as $key => $value)
{
$record[$key] = str_replace(","," ",$value);
$record[$key] = str_replace("\n"," ",$value);
$record[$key] = str_replace("\r"," ",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"a");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler","xml|meteobase/station/");
fclose($csv);
// include ('wetterdatensteuerskript.php');
// sleep (3);
?>
i dont understand the funktion with the
$record["STATION-NAME"]
ok thanks for help
Cheers Oli
and sorry my english is not so good
Hello Oli,
Perhaps can you tell me;
;Flueh;2009-09-07 11:15:00;5;;16.7;16.7;2009-09-07 11:15:00;16.7;2009-09-07 11:15:00;20.3;2009-09-06 17:10:00;14.5;2009-09-07 10:20:00;6.8;2009-09-07 07:00:00;6.8;2009-09-07 07:00:00;10.6;67;1021.9;0.0;0.0;0.0;0.0;32.7;2009-09-04 21:25:00;6.4;45;9.7;45;9.7;45;2009-09-07 11:15:00;9.7;45;2009-09-07 11:15:00;16.1;45;2009-09-06 14:50:00;16.7;16.7;558.0;3.4
What is wrong with it?
Can you post an example of how you do want it?
Thanks,
David.
Hi David
ok sorry
the outputfile dosend show my the rowheader from the xmlfile
like this
meteodata tp2m_5min_avg_cel
or from one older csv:
Datum,Zeit,Temp. I.,Temp. 2m A.,Temp. A. min,Temp. A. max,5cm Temp,Temp Hütte,Feuchte I.,Feuchte 2mA.,Regendauer,Luftdruck,Regen,Wind,Richtung,Sonnenschein,T Nass,ET,UV,Solar,Taupunkt,Windchill,Windböen,Empfang
,,°C,°C,°C,°C,°C,°C,%,%,lf,hPa,l/m²,km/h,°,min,min,mm,UV-I,W/m²,°C,°C,km/h,%
31.07.2009,15:42,26.3,22.7,22.7,22.7,0.0,0.0,46,51,0,1017.9,0.000,9.5,44,2,0,0.000,5.2,696,12.0,22.7,16.1,94.9
i hope your understand my
Thanks Oli
Hello Oli,
Yes - here is how to create a header row:
<?php
$xml = '{link saved}';
$csv = "meteobase_data.csv";
require("MagicParser.php");
$first = TRUE;
function myRecordHandler($record)
{
global $csv;
global $first;
// unset($record["ROW"]);
// strip commas, new-line and carriage return characters from all fields
if ($first)
{
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
$fields[] = $key;
}
fwrite($csv,implode(";",$fields)."\n");
}
foreach($record as $key => $value)
{
$record[$key] = str_replace(","," ",$value);
$record[$key] = str_replace("\n"," ",$value);
$record[$key] = str_replace("\r"," ",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"a");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler","xml|meteobase/station/");
fclose($csv);
// include ('wetterdatensteuerskript.php');
// sleep (3);
?>
See how it uses the $first variable to know when it is the first time, and then creates the header row from the $key values...
Hope this helps!
Cheers,
David.
Hi David,
im happy its running, thanks
no i have one problem more, it is possible to splitt the row data from the xml datetime in to date and time to the Outputfile ?
this where whry great.
Cheers an thanks
Oli
Hello Oli,
Do you mean to sort it by date and time?
Hi David
yes in to e new row of the csvfile: like date|time
cheers Oli
Hi David
yes in the xml is the date an time in the same row.
I nid in the csv two rows like date 07.09.2009 an time 10:55
i hope you understand my
Cheers Oli
Hello Oli,
There is quite a bit of tricky PHP required to do this, I'll look into it for you...
Cheers,
David.
Hi Oli,
I worked the code out for you, have a go with this:
<?php
$xml = "{link saved}";
$csv = "meteobase_data.csv";
require("MagicParser.php");
$first = TRUE;
function myRecordHandler($record)
{
global $csv;
global $first;
// split datetime fields up into separate date and time fields
foreach($record as $key => $value)
{
if (strpos($key,"DATETIME")!==FALSE)
{
$dateName = substr($key,0,-8)."DATE";
$timeName = substr($key,0,-8)."TIME";
$parts = explode(" ",$value);
$dateValue = $parts[0];
$timeValue = $parts[1];
unset($record[$key]);
$record[$dateName] = $dateValue;
$record[$timeName] = $timeValue;
}
}
// unset($record["ROW"]);
// strip commas, new-line and carriage return characters from all fields
if ($first)
{
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
$fields[] = $key;
}
fwrite($csv,implode(";",$fields)."\n");
}
foreach($record as $key => $value)
{
$record[$key] = str_replace(";"," ",$value);
$record[$key] = str_replace("\n","",$value);
$record[$key] = str_replace("\r","",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"w");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler","xml|meteobase/station/");
fclose($csv);
// include ('wetterdatensteuerskript.php');
// sleep (3);
?>
See the section with the "split datetime fields up into separate date and time fields" comment, what I have done is look for the field names including DATETIME, and then split them into 2, and unset() the original combined field, and then append the new separate fields onto the end of the array - so they will appear at the end of the row in your CSV.
Don't forget to change all of {link saved} to your URL!
Hope this helps!
Cheers,
David.
Hi David,
its working, put the output date is on thies way 2009-09-04 i must have 07.09.2009 and the time i dont nid the second like this 16:35
Cheers
Oli
Hello Oli,
Instead of:
$dateValue = $parts[0];
$timeValue = $parts[1];
Try this code:
$dateParts = explode("-",$parts[0]);
$dateValue = $dateParts[2].".".$dateParts[1].".".$dateParts[0];
$timeParts = explode(":",$parts[1]);
$timeValue = $timeParts[0].":".$timeParts[1];
Cheers,
David.
hi
david
sorry where i have to put in the code??
Cheers Oli
Hi David
ok this is my code to time:
<?php
$xml ='{link saved}';
$csv = "meteobase_data.csv";
require("MagicParser.php");
$first = TRUE;
function myRecordHandler($record)
{
global $csv;
global $first;
// split datetime fields up into separate date and time fields
foreach($record as $key => $value)
{
if (strpos($key,"DATETIME")!==FALSE)
{
$dateName = substr($key,0,-8)."DATE";
$timeName = substr($key,0,-8)."TIME";
$parts = explode(" ",$value);
//$dateValue = $parts[0];
//$timeValue = $parts[1];
$dateParts = explode("-",$parts[0]);
$dateValue = $dateParts[2].".".$dateParts[1].".".$dateParts[0];
$timeParts = explode(":",$parts[1]);
$timeValue = $timeParts[0].":".$timeParts[1];
unset($record[$key]);
$record[$dateName] = $dateValue;
$record[$timeName] = $timeValue;
}
}
// unset($record["ROW"]);
// strip commas, new-line and carriage return characters from all fields
if ($first)
{
// create the CSV header row on the first time here
$first = FALSE;
$fields = array();
foreach($record as $key => $value)
{
$fields[] = $key;
}
fwrite($csv,implode(";",$fields)."\n");
}
foreach($record as $key => $value)
{
$record[$key] = str_replace(";"," ",$value);
$record[$key] = str_replace("\n","",$value);
$record[$key] = str_replace("\r","",$value);
}
fwrite($csv,implode(";",$record)."\n");
}
$csv = fopen($csv,"w");
if (!$csv) { print "Could not create output file - check permissions!";exit(); }
MagicParser_parse($xml,"myRecordHandler","xml|meteobase/station/");
fclose($csv);
// include ('wetterdatensteuerskript.php');
// sleep (3);
?>
Oli
Hi Oli,
That looks OK!
Does it work how you want?
David.
hi
no the output file is the same as befour
STATION;STATION-NAME;STATION-DATETIME;STATION-INTERVALL_MIN;METEODATA;METEODATA-TP2M_5MIN_AVG_CEL;METEODATA-TP2M_1H_MAX_CEL;METEODATA-TP2M_1H_MAX_DATETIME;METEODATA-TP2M_TODAY_MAX_CEL;METEODATA-TP2M_TODAY_MAX_DATETIME;METEODATA-TP2M_24H_MAX_CEL;METEODATA-TP2M_24H_MAX_DATETIME;METEODATA-TP2M_1H_MIN_CEL;METEODATA-TP2M_1H_MIN_DATETIME;METEODATA-TP2M_TODAY_MIN_CEL;METEODATA-TP2M_TODAY_MIN_DATETIME;METEODATA-TP2M_24H_MIN_CEL;METEODATA-TP2M_24H_MIN_DATETIME;METEODATA-DWPT_5MIN_AVG_CEL;METEODATA-HYDR_5MIN_AVG_PER;METEODATA-BARO_5MIN_AVG_HPA;METEODATA-RR_SUM_MM;METEODATA-RR_SUM_1H_MM;METEODATA-RR_SUM_TODAY_MM;METEODATA-RR_SUM_24H_MM;METEODATA-RR_SUM_7D_MM;METEODATA-LAST_RR;METEODATA-WNDS_5MIN_AVG_KMH;METEODATA-WNDD_5MIN_AVG_DEG;METEODATA-WNDS_5MIN_MAX_KMH;METEODATA-WNDD_5MIN_MAX_DEG;METEODATA-WNDS_1H_MAX_KMH;METEODATA-WNDD_1H_MAX_DEG;METEODATA-WNDS_1H_MAX_DATETIME;METEODATA-WNDS_TODAY_MAX_KMH;METEODATA-WNDD_TODAY_MAX_DEG;METEODATA-WNDS_TODAY_MAX_DATETIME;METEODATA-WNDS_24H_MAX_KMH;METEODATA-WNDD_24H_MAX_DEG;METEODATA-WNDS_24H_MAX_DATETIME;METEODATA-WNTP_5MIN_AVG_CEL;METEODATA-HETP_5MIN_AVG_CEL;METEODATA-SOLR_5MIN_AVG_WM2;METEODATA-UVRD_5MIN_AVG_UV
;Flueh;2009-09-07 19:50:00;5;;17.0;19.1;2009-09-07 18:55:00;23.6;2009-09-07 17:15:00;23.6;2009-09-07 17:15:00;17.0;2009-09-07 19:50:00;6.8;2009-09-07 07:00:00;6.8;2009-09-07 07:00:00;12.3;74;1020.8;0.0;0.0;0.0;0.0;32.7;2009-09-04 21:25:00;0.0;270;1.6;270;4.8;270;2009-09-07 19:40:00;11.3;45;2009-09-07 14:05:00;11.3;45;2009-09-07 14:05:00;17.0;17.0;15.0;0.0
cheers Oli
Hi Oli,
When you said:
"its working, put the output date is on thies way 2009-09-04 i must have 07.09.2009 and the time i dont nid the second like this 16:35"
Did the csv look different then, but after this change it has gone back?
Hi David, no it has been no change, un time date amounts still.
greeting OLI
Hi Oli,
Can you email me your PHP code (as an attachment) and I will check it on my server!
Thanks,
David.
Hi David
i have send you the PHP code,
thanks fo your help
greets Oli
Hello Oli,
I have not received anything yet; I sent an email to you at the address registered on this forum, perhaps you could reply to that with your code as an attachment...
Cheers,
David.
Hello Oli,
The first thing to do is make sure that you are using the appropriate Format String in your call to MagicParser_parse(), and based on the XML example that you posted, I think this should be:
MagicParser_parse($xml,"myRecordHandler","xml|METEOBASE/STATION/");
With that in place, the station name attribute should appear in your CSV output, but if you want to access it directly within your myRecordHandler() function; it should be accessible through the variable:
$record["STATION-NAME"]
...for example:
print $record["STATION-NAME"];
Hope this helps,
Cheers,
David.