Jump to content

Recommended Posts

Hi Everyone!  Thank you in advance for reviewing my post and any assistance you may provide!

 

I am working on webscrapping the latest generator schedule from the TVA.  With the code that I am included below, I get random readings. Can anyone shine some light on how to grab the latest or first generator schedule?

 

Also can anyone show me how can I break this out to show each time segment?

 

Here is the link to the xml file:  http://www.tva.gov/lakes/xml/OHH_F.xml

 

 

<?php
$data = file_get_contents('http://www.tva.gov/lakes/xml/OHH_F.xml');
$regex = '@<GENERATORS>.*?</GENERATORS>@';
preg_match($regex, $data, $match);
$generators = substr(substr($match[0], 12), 0, -13);
print $generators;
?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/
Share on other sites

Don't use regular expressions when there are (much) better tools available. Like SimpleXML.

$xml = new SimpleXMLElement('http://www.tva.gov/lakes/xml/OHH_F.xml', 0, true);
foreach ($xml->ROW as $row) {
    printf("Release date: %s\n", $row->RELEASEDATE);
    printf("Time period: %s\n", $row->TIMEPERIOD);
    printf("Generators: %u\n", $row->GENERATORS); // or is this a bool?
}

That does seem to be more efficient! Thank you for the help and tips!  I have learned something.

 

One more question.  How do I segment this out so I can display the very first generators listed in the XML file?

 

 

Thank you again!

Mike

One more question.  How do I segment this out so I can display the very first generators listed in the XML file?

The first generators?

 

Even though $xml->ROW is multiple elements, if you treat it like it was just one then SimpleXML will give you back only the first one.

$xml->ROW->GENERATORS

Good catch.  Yes.  The very first generator.  Generators could be up to 4.

 

So what I am trying to do is list the current generator schedule... that is current.  The rest following are forecasted generator schedules.  Does that make sense?

 

 

The highlighed bold area is what I am trying to show since it is the current information.

 

<RESULTSET><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>noon - 2pm</TIMEPERIOD><GENERATORS>0</GENERATORS></ROW><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>2pm - 4pm</TIMEPERIOD><GENERATORS>1</GENERATORS></ROW><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>4pm - 6pm</TIMEPERIOD><GENERATORS>0</GENERATORS></ROW><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>6pm - 8pm</TIMEPERIOD><GENERATORS>1</GENERATORS></ROW><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>8pm - 10pm</TIMEPERIOD><GENERATORS>0</GENERATORS></ROW><ROW><RELEASEDATE>6/13/2012</RELEASEDATE><TIMEPERIOD>10pm - midnight</TIMEPERIOD><GENERATORS>1</GENERATORS></ROW></RESULTSET>

This is what I have per your recommendation.

 

Thanks!

Mike

 

 

<?php
//// SHOWS CURRENT AND FORECASTED GENERATOR SCHEDULE
$xml = new SimpleXMLElement('http://www.tva.gov/lakes/xml/OHH_F.xml', 0, true);
foreach ($xml->ROW->GENERATORS as $row) {
    printf("Generators: %u\n", $row->GENERATORS);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.