Jump to content

Grabbing the first entry of an XML file


michael.davis

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);
}
?>

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.