Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

I need your suggestions on the following code to read xml files inside of a zip file.

Posted on 2016-11-16
5
15 Views
Last Modified: 2016-11-21
This is the code.  It seems to work fine, but perhaps it can be improved.
#!/usr/local/bin/php
<?php
error_reporting(E_ALL);

// Download Currents Zip file
$url = "http://w1.weather.gov/xml/current_obs/all_xml.zip";
$filename = "all_xml.zip";
$retval = shell_exec("fetch $url 2>&1");		// Add error handling to make sure you get a new file

// Open Zip file
$file = getcwd() . '/' . $filename;				// Make up full path to file
$zip = zip_open($file);							// Return handle to file

// Loop here to read zip file and store new currents
  $zip_entry = zip_read($zip); 					// Return next directory entry or FALSE add code to break loop
  $xml_name = zip_entry_name($zip_entry);		// Return name of directory entry
  // Figure out how skip $xml_names that begin with a number.
  $retval = zip_entry_open($zip,$zip_entry);	// Open directory entry. Returns True on success.  Add error handling
  $xml = zip_entry_read ($zip_entry,1048576);	// Assign file content to string.  empty string EOF, or FALSE on error.  Add error handling
  $retval = zip_entry_close($zip_entry);		// Returns True on success.  Probably skip error handling.
  $obj = SimpleXML_Load_String($xml);			// Returns False on failure.  Add error handling
  // Code here to store new current in database
// End of loop

// Close Zip file
$retval = zip_close($zip);						// Returns True on success.  Probably skip error handling.

// **** Test area
// echo PHP_EOL . "Before XML dump";
// var_dump($xml);
// echo PHP_EOL . "After XML dump";
// echo PHP_EOL . "Before Obj dump";
var_dump($obj);
// echo PHP_EOL . "After Obj dump";

Open in new window

Thank you!
0
Comment
Question by:IT_Architect
  • 3
  • 2
5 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41889717
You could probably read the Zipped XML with file_get_contents() or cURL.  But if it works fine, why mess with it?

There are a ton of little files inside the zip archive.
0
 

Accepted Solution

by:
IT_Architect earned 0 total points
ID: 41889745
I think I'll stick with it.
- shell_exec gives me good error checking
- fetch automatically trashes the previous existing all_xml.zip
- zip_entry_read means I don't have to explode it into lots of files to be read from a directory
- It's definitely fast
- It works perfectly

Thanks tons...again :D
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41889772
As you wish... What are you going to do with all the little XML files?  There are a few thousand, I think.
0
 

Author Comment

by:IT_Architect
ID: 41889816
What are you going to do with all the little XML files?  There are a few thousand, I think.
They are ICAO current weather observations that we store in a MySQL database.  They are replaced every 15 minutes and individual requests don't make sense.  Since they are ICAOs, they are world-wide.

I would do bulk for the forecasts you helped me with earlier, but I cannot find a good source at Weather.gov for bulk forecasts, and the source I had where I could buy the information already consolidated stopped selling it and others are crazy expensive for free information.  Forecasts are updated once per hour, and I can pick up all zones in about 15 minutes doing only a few a second.  Forecasts cover only the US and it is one forecast per county.
0
 

Author Closing Comment

by:IT_Architect
ID: 41895656
Because it was agreed that it was good like it is.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question