作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的起始 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);
而 $parent
是 DOMElement
父级,(在您更新问题之后)获取是您问题的一部分:
// 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/
我是一名优秀的程序员,十分优秀!