dongyuli0964 2019-07-17 06:40
浏览 82
已采纳

为什么,我用注释$ 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'?>
  • 写回答

1条回答 默认 最新

  • dongque4778 2019-07-17 06:50
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误