简体   繁体   English

为什么,我用注释$ start var得到致命错误?

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

When I use $start var like this: 当我使用$ start var时:

$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 致命错误:未捕获异常:在/home/users/....../test.php:6中无法将字符串解析为XML堆栈跟踪:#0 /home/users/....../test。 php(6):SimpleXMLElement - > __ construct('<?xml version ='...')#1 {main}在第6行的/home/users/....../test.php中抛出

How can I generate nice formatted XML tag for XML? 如何为XML生成格式良好的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. 问题是<?xml version='1.0' encoding='utf-8'?/> (尽管它有一点点错误)实际上并没有定义XML元素,因为它没有根节点。

With your current code, you would have to add the <offers> element to the XML to give it the actual element content... 使用当前代码,您必须将<offers>元素添加到XML中,以便为其提供实际的元素内容...

$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... 作为您的其他版本的XML ...

$start = "<xml version='1.0' encoding='utf-8' />";

This actually declares a root node called <xml> . 这实际上声明了一个名为<xml>根节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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