You are here:  » How to Parse a TXT file by a wether Service?!


How to Parse a TXT file by a wether Service?!

Submitted by ifeo-admin on Thu, 2009-11-19 16:31 in

Hallo,

i have/try to parse the following TXT-file:
wetter_import.txt

Also i want to select each item manually like
Date: <WEAT_DATE>
Info: <WEAT_TODAY_D>
Temp1: <MOUN_TMW_2000_TMED>
Temp2: <MOUN_TMW_3000_TMED>
...
- is this possible and how?

A Result as Exampel:
http://www.jagdhof.com/de/wetter.php

Thamks and kind regards
O. Strmsek

Submitted by support on Thu, 2009-11-19 16:39

Hi There,

The link in your email is not valid XML i'm afraid.

However, it would be easy to turn it into valid XML by adding your own document element, and then parsing it at the document element (top) level would return the results you require - with every value in its own field!

Please try this:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
print_r($record);
  }
  
$url "http://cms.ifeo.de/wetter/wetter_import.txt";
  
$xml file_get_contents($url);
  
$xml "<wetter>".$xml."</wetter>";
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|WETTER/");
?>

This will display the record in its entirity using print_r(), and then you can go on to construct the HTML that you want to display using the actual variables, for example, to print the date on it's own:

  function myRecordHandler($record)
  {
    print $record["WEAT_DATE"];
  }

Hope this helps!
Cheers,
David.

Submitted by ifeo-admin on Thu, 2009-11-19 17:25

sry - i think i'am a newby
if i do some thing like this

<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    print_r($record);
  }
  $url = "wetter_import.txt";
  $xml = file_get_contents($url);
  $xml = "<wetter>".$xml."</wetter>";
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|WETTER/");
?>
<html><body>
My Text:..
<?php
  function myRecordHandler($record)
  {
    print $record["WEAT_DATE"];
  }
?>
</body></html>

So I get correctly the Error:
Fatal error: Cannot redeclare myrecordhandler()
(previously declared in /home/www/web1001/html/cms/wetter/test.php:3)
in /home/www/web1001/html/cms/wetter/test.php on line 18

How and there I have to define the html output, maybe is possible to post a running filecode?

Thanks!

P.S. Tell me there I have do Donate ;)

Submitted by support on Thu, 2009-11-19 17:30

Hi,

No problem! Here's how to combine the two;

<html><body>
My Text:..
<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    print $record["WEAT_DATE"];
  }
  $url = "http://cms.ifeo.de/wetter/wetter_import.txt";
  $xml = file_get_contents($url);
  $xml = "<wetter>".$xml."</wetter>";
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|WETTER/");
?>
</body></html>

Hope this helps,
Cheers,
David.

Submitted by ifeo-admin on Thu, 2009-11-19 17:37

Hy,

I gry the sam but its dosenot works - no array?!?

See:
big truble ;)

That is wrong?!

thanks!
Oliver

Submitted by support on Thu, 2009-11-19 18:05

Hello Oliver,

Not only is the XML not valid, but it also contains characeter encoding errors. I have a version of Magic Parser that can work around these error which I will email to you together with the source code that produces the full array as expected...

All the best,
David.

Submitted by ifeo-admin on Thu, 2009-11-19 19:35

Hy - perfekt,

yes now works. But that is the prob because the charset, i change it now to ISO-8859-1, so the file is readable (http://cms.ifeo.de/wetter/wetter_import.txt), the output not (http://cms.ifeo.de/wetter/test/ifeo.php).

Is there a possibility to also set the charset in the output.

thank a lot
Oliver

Submitted by support on Thu, 2009-11-19 19:38

Hi Oliver,

Make sure at the top of your script (ifeo.php) you send the content-type header to set the charset, like this:

  header("Content-Type: text/html; charset=ISO-8859-1");

(make that the first line of your PHP)

Cheers!
David.