4

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 &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
 </product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; 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 &amp; GPS</parentCategory>
<Category>Car Installation Parts</Category>
<subCategory>Deck Installation Parts</subCategory>
<sisterCategory>Antennas &amp; Adapters</sisterCategory></product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
</products>

As you can see, it only replaces one instance of product.

0

1 Answer 1

4

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.