Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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-在元素XML中添加新元素_Php_Xml - Fatal编程技术网

PHP-在元素XML中添加新元素

PHP-在元素XML中添加新元素,php,xml,Php,Xml,我正在尝试使用PHP(XML)在父元素中添加多个元素: 这就是我努力实现的目标: <ns0:XmlInterchange xmlns:ns0="http://www.edi.com.au/EnterpriseService/" xmlns:ext="http://esb.dsv.com/ExtensionFunctions"> <ns0:InterchangeInfo> <ns0:Date>2017-07-20 13:41:48</ns0:

我正在尝试使用PHP(XML)在父元素中添加多个元素:

这就是我努力实现的目标:

<ns0:XmlInterchange xmlns:ns0="http://www.edi.com.au/EnterpriseService/" xmlns:ext="http://esb.dsv.com/ExtensionFunctions">
  <ns0:InterchangeInfo>
     <ns0:Date>2017-07-20 13:41:48</ns0:Date>
     <ns0:XmlType>Verbose</ns0:XmlType>
     <ns0:Source>
        <ns0:EnterpriseCode>Company</ns0:Enterprisecode>
     </ns0:Source>
 </ns0:InterchangeInfo>
</ns0:XmlInterchange>

2017-07-20 13:41:48
冗长的
单位
我有以下PHP代码:

$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;

$interchange = $xml->createElementNS('ns0', 'ns0:XmlInterchange');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns0', 'http://www.edi.com.au/EnterpriseService/');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ext', 'http://esb.dsv.com/ExtensionFunctions');
$xml->appendChild($interchange); //Add above attributes to our element. <!Xmlinterchange->

$item = $xml->createElement('ns0:InterchangeInfo');
$item->appendChild($xml->createElement('ns0:Date',$invoice_date));
$item->appendChild($xml->createElement('ns0:XmlType','Verbose'));
$item = $xml->createElement('ns0:Source');
$item->appendChild($xml->createElement('ns0:EnterpriseCode','Company'));
$interchange->appendChild($item);
unset($item); //Reset $item, so we can use the variable again.
$xml=newDOMDocument('1.0','UTF-8');
$xml->formatOutput=true;
$interchange=$xml->createElementNS('ns0','ns0:XmlInterchange');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/'、'xmlns:ns0'、'http://www.edi.com.au/EnterpriseService/');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/“,”xmlns:ext“,”http://esb.dsv.com/ExtensionFunctions');
$xml->appendChild($interchange)//将上述属性添加到元素中。
$item=$xml->createElement('ns0:InterchangeInfo');
$item->appendChild($xml->createElement($ns0:Date',$invoice_Date));
$item->appendChild($xml->createElement('ns0:XmlType','Verbose');
$item=$xml->createElement('ns0:Source');
$item->appendChild($xml->createElement('ns0:EnterpriseCode','Company');
$interchange->appendChild($item);
未结算(项目)//重置$item,以便再次使用该变量。
然而,上述产出:

<ns0:XmlInterchange xmlns:ns0="http://www.edi.com.au/EnterpriseService/" xmlns:ext="http://esb.dsv.com/ExtensionFunctions">
  <ns0:Source>
    <ns0:EnterpriseCode>Company</ns0:EnterpriseCode>
  </ns0:Source>
</ns0:XmlInterchange>

单位

我希望能够使
元素位于
元素中,但与其他元素一起。

您必须将
附加到
中。之后,将
附加到

请记住:
->appendChild()
的意思不是附加到,而是附加到内部。所以
$a->appendChild($b)表示在a中附加b

尝试:


我应该如何附加它?你能举个例子吗?我应该仍然使用$item变量吗?更新了我的帖子。希望它是正确的。只有当所有的子对象都设置正确时,才添加一个对象。上面的输出与我得到的相同。它删除
并将其替换为
,您可以在那里覆盖
$item
。我使用了一个新的变量名。试试这个。
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;

$interchange = $xml->createElementNS('ns0', 'ns0:XmlInterchange');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns0', 'http://www.edi.com.au/EnterpriseService/');
$interchange->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ext', 'http://esb.dsv.com/ExtensionFunctions');

$item = $xml->createElement('ns0:InterchangeInfo');
$item->appendChild($xml->createElement('ns0:Date',$invoice_date));
$item->appendChild($xml->createElement('ns0:XmlType','Verbose'));

$source = $xml->createElement('ns0:Source');
$source->appendChild($xml->createElement('ns0:EnterpriseCode','Company'));
$item->appendChild($source);

$interchange->appendChild($item);

$xml->appendChild($interchange);