You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中XML的addChild方法命名空间缺失问题求助

PHP中XML的addChild方法命名空间缺失问题求助

有没有大佬能帮忙分析下这到底是咋回事啊?我在代码其他地方用完全一样的语法调用addChild都没出过问题,但这段代码就是不按预期工作。

我当前的代码是这样的:

$sale = $xml->addChild('sale', null, 'http://www.capita-software-services.com/scp/simple');
$sale->addChild('saleSummary', null, 'http://www.capita-software-services.com/scp/base');
$sale->addChild('description', $orderDescription);
$sale->addChild('amountInMinorUnits', intval($order->get_total() * 100));

原本预期能生成这样的XML:

<sale xmlns="http://www.capita-software-services.com/scp/simple">
   <saleSummary xmlns="http://www.capita-software-services.com/scp/base"/>
   <description>Order from Payment Portal</description>
   <amountInMinorUnits>2000</amountInMinorUnits>
</sale>

但实际输出的XML里,命名空间相关的内容并没有按预期渲染出来,要么是父元素的命名空间没生效,要么是子元素的命名空间丢失了,实在搞不懂哪里出问题了😭

备注:内容来源于stack exchange,提问作者RussDCC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 14:19:30