Reputation: 163
I'm using this PHP code to replace a series of nodes in an XML file:
<?php
$xml = "
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
</products>
";
function renameTags($xml, $old, $new)
{
$dom = new DOMDocument();
$dom->loadXML($xml);
$nodes = $dom->getElementsByTagName($old);
$toRemove = array();
foreach ($nodes as $node)
{
$newNode = $dom->createElement($new);
foreach ($node->attributes as $attribute)
{
$newNode->setAttribute($attribute->name, $attribute->value);
}
foreach ($node->childNodes as $child)
{
$newNode->appendChild($node->removeChild($child));
}
$node->parentNode->appendChild($newNode);
$toRemove[] = $node;
break;
}
foreach ($toRemove as $node)
{
$node->parentNode->removeChild($node);
}
$dom->formatOutput = TRUE;
return $dom->saveXML();
}
$xml = renameTags($xml, 'name', 'Source');
$xml = renameTags($xml, 'name', 'parentCategory');
$xml = renameTags($xml, 'name', 'Category');
$xml = renameTags($xml, 'name', 'subCategory');
$xml = renameTags($xml, 'name', 'sisterCategory');
echo $xml;
?>
However, this code only replaces and only works for one product set. I think I need to call a foreach statement somewhere in the function that consists of foreach parentNode('product') replace tag names. What do I need to add for the function to iterate over all sets of product?
The above code will print this: (notice that the first set of name nodes are gone, and are replaced by the specified new nodes)
<?xml version="1.0"?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<Source>Buy</Source>
<parentCategory>Car, Marine & GPS</parentCategory>
<Category>Car Installation Parts</Category>
<subCategory>Deck Installation Parts</subCategory>
<sisterCategory>Antennas & Adapters</sisterCategory></product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
</products>
As you can see, it only replaces one instance of product.
Upvotes: 4
Views: 1355
Reputation: 381
The break in that foreach was your problem: it was stopping at the first <name>
node it ran into, regardless of where it was in the document.
First you'd have to loop through the products, then through the the product's <name>
nodes. Something like the following:
function renameTags($xml, $old, $new) {
$toRemove = array();
$dom = new DOMDocument();
$dom->loadXML($xml);
$products = $dom->getElementsByTagName('product');
foreach($products as $product) {
$nodes = $product->getElementsByTagName($old);
foreach ($nodes as $node) {
$newNode = $dom->createElement($new);
foreach ($node->attributes as $attribute) {
$newNode->setAttribute($attribute->name, $attribute->value);
}
foreach ($node->childNodes as $child) {
$newNode->appendChild($node->removeChild($child));
}
$node->parentNode->appendChild($newNode);
$toRemove[] = $node;
break;
}
}
foreach ($toRemove as $node) {
$node->parentNode->removeChild($node);
}
$dom->formatOutput = TRUE;
return $dom->saveXML();
}
Upvotes: 4