【发布时间】:2015-02-11 13:46:17
【问题描述】:
所以我有一个像这样的 Xml 文件
<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>
所以我需要将所有标签名称更改为替代,我需要得到类似的东西
<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>
【问题讨论】: