所以我有这样PHP DOM的Xml循环throught元素保持儿童
<cars>
<id>1</id>
<photos>
<img>http://sit.com/img.jpg</img>
<img>http://sit.com/img.jpg</img>
<img>http://sit.com/img.jpg</img>
<img>http://sit.com/img.jpg</img>
</photos>
</cars>
一个XML文件,所以我需要所有的标签名称更改为替代,我需要得到像
<cars>
<ex_id>1</ex_id>
<images>
<photo>http://sit.com/img.jpg</photo>
<photo>http://sit.com/img.jpg</photo>
<photo>http://sit.com/img.jpg</photo>
<photo>http://sit.com/img.jpg</photo>
</images>
</cars>
我的代码是
foreach ($dom->getElementsByTagName('cars') as $item) {
for ($i = 0; $i < $item->childNodes->length; ++$i) {
$car = $item->childNodes->item($i);
$NewElement = $dom->createElement($newName,$value);
$car->parentNode->replaceChild($NewElement->cloneNode(TRUE), $car);
}
}
做这样的事情
<cars>
<ex_id>1</ex_id>
<images/>
</cars>
所以切<photos>
所有儿童,所以我的问题是如何保护孩子,改变儿童的标签从<img>
到<photo>
感谢您的回答,但我认为你不明白我想要做什么。我不能改变我的代码,因为那时我需要改变我的所有项目。我只需要添加两个字符串到我现有的代码来解决我的问题 – 2015-02-11 14:21:15