gpt4 book ai didi

php - 如何将 child 添加到特定节点

转载 作者:行者123 更新时间:2023-12-05 09:24:45 26 4
gpt4 key购买 nike

这是我的起始 xml:

 <?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<children></children>
</child>
</root>

我想在 child 中添加其他 child ??但我正在尝试,没有什么我不知道如何...:S help pls 我想让我的 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);

最佳答案

DOMDocument就这么简单:

$child = new DOMElement('children');
$parent->appendChild($child);

$parentDOMElement 父级,(在您更新问题之后)获取是您问题的一部分:

// append <children> to the first <child> element
$parent = $dom->getElementsByTagName('child')->item(0);
$child = new DOMElement('children');
$parent->appendChild($child);

关于php - 如何将 child 添加到特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706558/

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