gpt4 book ai didi

php - SimpleXMLElement::addChild() 不工作

转载 作者:搜寻专家 更新时间:2023-10-31 20:35:04 27 4
gpt4 key购买 nike

在我的 php 代码中,我有两个变量 $parent$child。他们都有一些 xml 数据。在 $parent 变量中有一个名为 <payheads> 的标签,它可能包含多个 <payhead> 标签。在 $child 变量中只有一个 <payhead> 标签。我想要的是,我想在 <payheads> 变量的 $parent 标签的末尾添加那个 child 。我的代码如下:

<?php

$parent = new SimpleXMLElement('<review_sr><status>Review Complete</status><payheads><payhead><code>ABAS</code><old_value>0.00</old_value><new_value>63570.00</new_value><review_id>1234567890</review_id><review_time>20160614:11:00:47</review_time><status>Accepted</status></payhead></payheads><current_gross_allowance>50481.00</current_gross_allowance><review_gross_allowance>114051.00</review_gross_allowance></review_sr>');

$child = new SimpleXMLElement('<source><payhead><code>DMG</code><old_value>500.00</old_value><new_value>0.00</new_value><review_id>1234567890</review_id><review_time>20160620:12:41:17</review_time><status>Accepted</status></payhead></source>');
$child = $child->xpath('//payhead'); //This is to ignore the xml declaration that is automatically produced by the SimpleXMLElement constructor
$child = $child[0]; //I've tried LIBXML_NOXMLDECL but its not working, that's why these two lines.

//var_dump($parent);
//var_dump($child->asXML());

$parentNode = $parent->xpath('//payheads');
$parentNode[0]->addChild('payhead', $child);
//var_dump($parentNode[0]);
foreach($parent as $key=>$value)
foreach($value as $key=>$value)
var_dump($value); //only 1 payhead is shown. but there should be 2 of them.

?>

实际输出:

object(SimpleXMLElement)[5]
public 'code' => string 'ABAS' (length=4)
public 'old_value' => string '0.00' (length=4)
public 'new_value' => string '63570.00' (length=8)
public 'review_id' => string '1234567890' (length=10)
public 'review_time' => string '20160614:11:00:47' (length=17)
public 'status' => string 'Accepted' (length=8)

object(SimpleXMLElement)[6]

预期输出:

object(SimpleXMLElement)[5]
public 'code' => string 'ABAS' (length=4)
public 'old_value' => string '0.00' (length=4)
public 'new_value' => string '63570.00' (length=8)
public 'review_id' => string '1234567890' (length=10)
public 'review_time' => string '20160614:11:00:47' (length=17)
public 'status' => string 'Accepted' (length=8)

object(SimpleXMLElement)[6]
public 'code' => string 'DMG' (length=4)
public 'old_value' => string '500.00' (length=4)
public 'new_value' => string '0.00' (length=8)
public 'review_id' => string '1234567890' (length=10)
public 'review_time' => string '20160620:12:41:17' (length=17)
public 'status' => string 'Accepted' (length=8)

我做错了什么?

最佳答案

addChild 采用 string 参数 - 第一个是要创建的标签名称,(可选)第二个是它的文本内容,(可选)第三个是命名空间元素的。

您不能只将另一个元素传递给它来代替使用。看起来 SimpleXMLElement 对于这种操作来说实际上太简单了。

相反,请考虑使用 DOMDocument,如下所示:

<?php
$parent = new DOMDocument();
$parent->loadXML('<review_sr><status>Review Complete</status><payheads><payhead><code>ABAS</code><old_value>0.00</old_value><new_value>63570.00</new_value><review_id>1234567890</review_id><review_time>20160614:11:00:47</review_time><status>Accepted</status></payhead></payheads><current_gross_allowance>50481.00</current_gross_allowance><review_gross_allowance>114051.00</review_gross_allowance></review_sr>');

$child = new DOMDocument();
$child->loadXML('<source><payhead><code>DMG</code><old_value>500.00</old_value><new_value>0.00</new_value><review_id>1234567890</review_id><review_time>20160620:12:41:17</review_time><status>Accepted</status></payhead></source>');
$childNode = $child->getElementsByTagName('payhead')->item(0);

$parentNode = $parent->getElementsByTagName('payheads')->item(0);
$parentNode->appendChild($child);
foreach($parent->getElementsByTagName('payhead') as $payhead) {
var_dump($payhead);
}

如果需要,您可以获取这些 payheads 并将它们传递给 SimpleXMLElement 以非常简单的方式获取它们的属性:

$simplePayhead = simplexml_import_dom($payhead);
var_dump($simplePayhead);

关于php - SimpleXMLElement::addChild() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917475/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com