Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SimpleXMLElement::addChild()不工作_Php - Fatal编程技术网

Php SimpleXMLElement::addChild()不工作

Php SimpleXMLElement::addChild()不工作,php,Php,在我的php代码中,我有两个变量$parent和$child。它们都有一些xml数据。在$parent变量中有一个名为的标记,它可能包含多个标记。在$child变量中只有一个标记。我想要的是,我想在$parent变量的标记的末尾添加该子项。我的代码如下: <?php $parent = new SimpleXMLElement('<review_sr><status>Review Complete</status><payheads><

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

<?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)
预期产出:

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
接受字符串参数-第一个是要创建的标记名,(可选)第二个是它的文本内容,(可选)第三个是元素的名称空间

您不能只是将另一个元素传递给它以供使用。对于这种操作,
simplexmlement
似乎实际上太简单了

取而代之的是,考虑使用<代码> DOMBODC/<代码>,类似于:

<?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);
}
如果需要,您可以将这些工资单传递给
simplexmlement
,以非常简单的方式获取其属性:

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

请试试这个。问题是simplexmlelement的
addChild()
需要一个字符串,而您正试图附加一个它类型转换为null的对象

公共SimpleXMLElement SimpleXMLElement::addChild(字符串$name[,字符串$value[,字符串$namespace]])

您需要使用DOM并将子对象添加到DOM中

function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
    $toDom = dom_import_simplexml($to);
    $fromDom = dom_import_simplexml($from);
    $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}

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

$parentNode = $parent->xpath('//payheads');
sxml_append($parentNode[0],$child[0]);

var_dump($parent);

是的,我现在就是这样做的。我只是想知道为什么我的代码不起作用。simplexmlelement的addChild()需要一个字符串,而您正试图附加一个它类型转换为null的对象。您需要使用DOM并将子对象追加到DOM中。