简体   繁体   中英

Why, I am getting a Fatal Error with commented $start var?

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'?>

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM