Jump to content

Lake Levels XML Code Parsing


michael.davis

Recommended Posts

Happy Monday All!

 

I am writing some code to try to pull in some XML from the TVA on lake levels.  This file is updated about every 3 to 4 hours.  I have the startings to pull the data in, but running into some problems.  Here is a break down of the problems.  I am fairly new to PHP, but trying to learn as I go.  Any help would be greatly appreciated.

 

[*]The code I have will not pull in the data and display

[*]One I am able to display the code, I need to review the upsteam elevation and compare to the previous levels and show the lake rising, falling, or stable.

Here is my code:

 

<?php
$html = file_get_contents('http://www.tva.gov/lakes/xml/OHH_R.xml');
$xml = simplexml_load_file('$html');
echo $html->getName() . "<br />";
foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
?> 

 

 

I am fairly new to PHP, but trying to learn as I go.  Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/254661-lake-levels-xml-code-parsing/
Share on other sites

Thanks, scootstah.  I dont have access to a database.  I was simply going to use the data that was already available.  Check out the XML code that I am trying to source from:

 

I am keying on the time and the values like this:  <UPSTREAM_ELEV>  444.61</UPSTREAM_ELEV>

 

<DBI driver="rvsp"><RESULTSET>
</RESULTSET><RESULTSET><ROW><OBS_DAY>1/08/2012</OBS_DAY><OBS_HR>11 pm</OBS_HR><UPSTREAM_ELEV>   444.61</UPSTREAM_ELEV><DOWNSTREAM_ELEV>   401.51</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  37,200</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/08/2012</OBS_DAY><OBS_HR>midnight</OBS_HR>[b]<UPSTREAM_ELEV>   444.61</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.51</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  37,200</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>1 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.80</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.20</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>2 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.80</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.20</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>3 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.85</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.15</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>4 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.85</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.25</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>5 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.90</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.10</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW><ROW><OBS_DAY>1/09/2012</OBS_DAY><OBS_HR>6 am</OBS_HR>[b]<UPSTREAM_ELEV>   444.90</UPSTREAM_ELEV>[/b]<DOWNSTREAM_ELEV>   401.10</DOWNSTREAM_ELEV><AVG_HOURLY_DISCHARGE>  36,400</AVG_HOURLY_DISCHARGE></ROW></RESULTSET></DBI>

Ok this is what I have come up with so far.  I used an old script that I had to pull in Weather Data from the Storm Prediction Center.  I have modified it to pull the upstream lake levels in. I have that part working.  8)

 

Now to my issue.

 

What I am wanting to do is show if the levels are rising, falling or stable.  Would also like to show the departure from the normal level.  Can anyone help?

 

<?php

$tvaread_insideitem = false;
$tvaread_tag = "";
$tvaread_title = "";
$tvaread_description = "";
$tvaread_link = "";

function tva_startElement($tvaread_parser, $tvaread_name, $tvaread_attrs) {
global $tvaread_insideitem, $tvaread_tag, $tvaread_title,
	$tvaread_description, $tvaread_link;

if ($tvaread_insideitem) {
	$tvaread_tag = $tvaread_name;
} elseif ($tvaread_name == "ROW") {
	$tvaread_insideitem = true;
}
}

function tva_endElement($tvaread_parser, $tvaread_name) {
global $tvaread_insideitem, $tvaread_tag, $tvaread_title,
	$tvaread_description, $tvaread_link;
if ($tvaread_name == "ROW") {

	$title = split(' *- *', $tvaread_title);
	$font = split(',', $color_table[$title[0]]);

	printf("<a target='_blank' href='%s'>%s%s%s</a><br>",
		trim($tvaread_link),
		$font[0],
		htmlspecialchars(trim($tvaread_title)),
		$font[1]);
	//printf("<span class='BodyText'>%s</span><br>",
	//	htmlspecialchars(trim($tvaread_description)));
	$tvaread_title = "";
	$tvaread_description = "";
	$tvaread_link = "";
	$tvaread_insideitem = false;
}
}

function tva_characterData($tvaread_parser, $tvaread_data) {
global $tvaread_insideitem, $tvaread_tag, $tvaread_title,
	$tvaread_description, $tvaread_link;

if ($tvaread_insideitem) {
	switch ($tvaread_tag) {

		case "UPSTREAM_ELEV":
		$tvaread_title .= $tvaread_data;
		break;

		//case "UPSTREAM_ELEV":
		//$tvaread_description .= $tvaread_data;
		//break;

		//case "AVG_HOURLY_DISCHARGE":
		//$tvaread_link .= $tvaread_data;
		//break;
	}
}
}

$tvaread_xml_parser = xml_parser_create();
xml_set_element_handler($tvaread_xml_parser,
"tva_startElement", "tva_endElement");

xml_set_character_data_handler($tvaread_xml_parser, "tva_characterData");

$tvaread_fp = fopen("http://www.tva.gov/lakes/xml/OHH_R.xml","r")
or die("Error reading RSS data.");

while ($tvaread_data = fread($tvaread_fp, 4096)) {
xml_parse($tvaread_xml_parser, $tvaread_data, feof($tvaread_fp))
or die(sprintf("XML error: %s at line %d",
	xml_error_string(xml_get_error_code($tvaread_xml_parser)),
	xml_get_current_line_number($tvaread_xml_parser)));
}

fclose($tvaread_fp);
xml_parser_free($tvaread_xml_parser);

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.