Reputation: 53
I know many questions like this have been asked, I've read many answers and still keep getting errors. I created a simple version for testing purposes and get the same errors as my main script. I'm sure it's something simple but I've been fighting with it for hours and am losing hair over it.
Demo XML file generated in PHP (this one is static to simulate what's generated by my real file, testing on this file got the errors below):
<?php
header("Content-type: text/xml");
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "
<users>
<user>
<search_tkID>5</search_tkID>
<search_connection>1</search_connection>
<search_name>Daniel Appleseed</search_name>
<search_pic>0</search_pic>
</user>
<user>
<search_tkID>4</search_tkID>
<search_connection>1</search_connection>
<search_name>Danny Apple</search_name>
<search_pic>0</search_pic>
</user>
<count>
<number>2</number>
</count>
</users>";
?>
PHP file attempting to read in this file:
$path = "xml/xml_test.php";
$xml_search = simplexml_load_file($path);
print_r($xml_search);
Error messages output by the PHP page:
Warning: simplexml_load_file() [function.simplexml-load-file]: xml/xml_test.php:5: parser error : Start tag expected, '<' not found in MYPAGE.php on line 16
Warning: simplexml_load_file() [function.simplexml-load-file]: print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; in MYPAGE.php on line 16
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in MYPAGE.php on line 16
Upvotes: 0
Views: 2055
Reputation: 53
The answer: I need to read in the URL, not just the file name, to execute the PHP that will generate the XML output than will be read in by simpleXML. Such a minor difference got the best of me for hours. No relative paths allowed if you want to execute the PHP.
$path = "http://wwww.site.com/xml/xml_test.php";
$xml_search = simplexml_load_file($path);
print_r($xml_search);
Upvotes: 0
Reputation: 198119
You XML document is invalid. A valid XML document optionally start with the XML declaration and hast at least one document element.
The document you have only consists of a single processing instruction. That is invalid:
<?php
...
?>
For SimpleXML to load and parse the document you would need to at least wrap it with such a document element, I took the liberty to name it <doc>
in the following example:
$path = "xml/xml_test.php";
$buffer = file_get_contents($path);
$xml_search = simplexml_load_string("<doc>$buffer</doc>");
print_r($xml_search);
Which should show some output along the lines:
SimpleXMLElement Object
(
[php] => SimpleXMLElement Object
(
)
[users] => SimpleXMLElement Object
(
...
And the actual XML document can be output as well (I suggest this over print_r
as the output of print_r
can be a little misleading with SimpleXMLElements:
$xml_search->asXML('php://output');
Output:
<?xml version="1.0"?>
<doc><?php header("Content-type: text/xml");
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "
<users>
<user>
...
I hope you now better understand which actual XML document you were trying to load.
In the end it might be better you store the XML verbatim on disk as a .xml
file and just load it in both PHP files.
File xml/xml_test.php
:
<?php
header("Content-type: text/xml");
readfile('result.xml');
The other file:
$path = "xml/result.xml";
$xml_search = simplexml_load_file($path);
...
This most likely matches your expectations better I guess.
Upvotes: 1