这是我的起始 xml :
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<children></children>
</child>
</root>
我想在孩子中添加其他孩子?但我正在尝试,但我不知道如何...:请帮助我,我希望我的 xml 是这样的
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<children></children>
<children></children>
</child>
</root>
这是我的代码:
<?php
$dom = new DomDocument;
//$dom->preserveWhiteSpace = FALSE;
$dom->load("myXML.xml");
$root = $dom->getElementById('root');
$params = $dom->getElementsByTagName('child');
$cantidadCategorias = $params->length+1;
$newElement = $dom->createElement('child','');
$dom->appendChild($newElement);
$f = fopen("myXML.xml",'w+');
fwrite($f,$dom->saveXML());
fclose($f);
With DOMDocument就这么简单:
$child = new DOMElement('children');
$parent->appendChild($child);
Whereas $parent
is the DOMElement
父母(在您更新问题后)要获取的内容是您问题的一部分:
// append <children> to the first <child> element
$parent = $dom->getElementsByTagName('child')->item(0);
$child = new DOMElement('children');
$parent->appendChild($child);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)