michael.davis Posted June 13, 2012 Share Posted June 13, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/ Share on other sites More sharing options...
requinix Posted June 13, 2012 Share Posted June 13, 2012 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? } Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353559 Share on other sites More sharing options...
michael.davis Posted June 13, 2012 Author Share Posted June 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353562 Share on other sites More sharing options...
requinix Posted June 13, 2012 Share Posted June 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353565 Share on other sites More sharing options...
michael.davis Posted June 13, 2012 Author Share Posted June 13, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353567 Share on other sites More sharing options...
michael.davis Posted June 13, 2012 Author Share Posted June 13, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353569 Share on other sites More sharing options...
michael.davis Posted June 13, 2012 Author Share Posted June 13, 2012 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264125-grabbing-the-first-entry-of-an-xml-file/#findComment-1353571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.