Reputation: 53
When I use $start var like this:
$start = "<?xml version='1.0' encoding='utf-8'?/>";
I get a fatal error:
Fatal error: Uncaught Exception: String could not be parsed as XML in /home/users/....../test.php:6 Stack trace: #0 /home/users/....../test.php(6): SimpleXMLElement->__construct('<?xml version='...') #1 {main} thrown in /home/users/....../test.php on line 6
How can I generate nice formatted XML tag for XML?
$start = "<xml version='1.0' encoding='utf-8' />";
//$start = "<?xml version='1.0' encoding='utf-8'?/>";
$gmXML = new SimpleXMLElement($start);
$offers = $gmXML->addChild('offers');
Header('Content-type: text/xml;');
echo $gmXML->asXML();
What I need is:
<?xml version='1.0' encoding='utf-8'?>
Upvotes: 1
Views: 55
Reputation: 57131
The problem is that <?xml version='1.0' encoding='utf-8'?/>
(although with a slight mistake in it anyway) doesn't actually define an XML element as it has no root node.
With your current code, you would have to add the <offers>
element to the XML to give it the actual element content...
$start = "<?xml version='1.0' encoding='utf-8'?><offers />";
$gmXML = new SimpleXMLElement($start);
Header('Content-type: text/xml;');
echo $gmXML->asXML();
Also note the end of the definition is ?>
and not ?/>
As your other version of the XML...
$start = "<xml version='1.0' encoding='utf-8' />";
This actually declares a root node called <xml>
.
Upvotes: 3