1

How to create XML reader from the URL XML data? I provide valid XML data from a URL to the PHPExcel factory's identify() but the script fires an error:

( ! ) Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message ' in C:\wamp\www\project\Classes\PHPExcel\Reader\Excel2007.php on line 82

( ! ) PHPExcel_Reader_Exception: Could not open for reading! File does not exist. in C:\wamp\www\project\Classes\PHPExcel\Reader\Excel2007.php on line 82

$url = "http://www.w3schools.com/xml/note.xml";
$xml = simplexml_load_file($url); //OR $xml = simplexml_load_string(file_get_contents($url));
$inputFileType = PHPExcel_IOFactory::identify($xml); // ERROR

UPDATE:

$dom = new DOMDocument();
$dom->load($url);
$fileName = 'filename.xml';
$xml = $dom->save($fileName);
$inputFileType = PHPExcel_IOFactory::identify($xml);

( ! )Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open 116752 for reading! File does not exist.' in C:\wamp\www\project\Classes\PHPExcel\Reader\Excel2007.php on line 82

( ! ) PHPExcel_Reader_Exception: Could not open 116752 for reading! File does not exist.

4
  • Regarding your edit: you are saving the file to $fileName but passing PHPExcel a different variable. You don't actually need to parse the XML file at all, just download it so you have a filename to give PHPExcel. Commented May 23, 2014 at 20:45
  • I have no idea what you're expecting PHPExcel to do with that file, though. Expecting it to read any XML file and turn it into a useful spreadsheet is a bit like expecting someone to give a useful answer to any possible question in English. Commented May 23, 2014 at 20:50
  • I think I got it. I was passing a file instead of filename to the file itself for the identify(). I'll try saving it as Excel now. What I want to do is basically grab that XML from URL, save it as XML file on server(because PHPExcel seems to be not able to read data from URLs, then open that locally saved XML and save it as Excel). Seems kind of unnecessary complex, but I think that's because of PHPExcel's shortfalling(I'm not sure, maybe there's a way to read XML from URL right into identify) Commented May 23, 2014 at 20:59
  • You don't need SimpleXML or DOM just to download the file, though: you could use file_get_contents() and file_put_contents(), for instance. There's nothing special about it being XML at that point. Commented May 23, 2014 at 21:03

2 Answers 2

3

The XML Reader (PHPExcel_Reader_Excel2003XML) that is included with PHPExcel isn't for any arbitrary XML file: that would require an incredible degree of machine intelligence to parse a file of arbitrary structure to a the determinate structure of a spreadsheet. A generic XML Reader that can take any structure of XML file and import it into an Excel document without comprehension of the structure of the XML simply isn't possible without writing your own code.

MS Excel 2003 supported a format called SpreadsheetML, which was a zip-compressed XML file with a defined structure and - while the format is very rarely used - the PHPExcel_Reader_Excel2003XML provides support for spreadsheet files written using that format. The XMLReader.php file in the /Examples folder demonstrates reading a SpreadsheetML file.

Details of SpreadsheetML can be found here

EDIT

Note also that all PHPExcel Readers expect a filename as their argument, none will accept raw data in any format, or any PHP Objects

Sign up to request clarification or add additional context in comments.

Comments

1

PHPExcel is to read Excel files, not XML. Try SimpleXML extension.

UPDATE:

So C:\wamp\www\project\Classes\PHPExcel\Reader\Excel2007.php on line 82 says this:

if (!file_exists($pFilename)) {
    throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}

Which means that it expects $pFilename to point on actual file on the filesystem. Try saving your $xml data somewhere and supplying filename to the PHPExcel_IOFactory::identity().

5 Comments

Well, it isn't that straightforward, unfortunately. You probably will have to write bit more code to iterate through the dataset in XML and (row by row) write it into XLS file. SimpleXML extension can help you read and parse XML file, and PHPExcel library could be indeed used to create and save XLS. But again - it will require a bit more coding.
UPDATE: I've found XMLReader.php example in Examples folder of PHPExcel. Is it some different XML type?
Would I need to parse through XML?
What I meant to ask is why do I need to parse XML with SimpleXML? Simply getting the XML from that website and saving it as .xls file would be what I'm looking to do. SimpleXML therefore takes care of the first step by loading XML from URL.
I tried using DOM. Please view my original question. I've added the piece of code I've used. It does save the XML data as .xml file properly, although identify() still throws the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.