Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:18:02+00:00 2026-05-27T11:18:02+00:00

I have an XML file that currently has 4 nodes with the same name:

  • 0

I have an XML file that currently has 4 nodes with the same name:
The file looks like this: (data.xml)

<?xml version="1.0"?>
<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>
</products>

There are 4 nodes with the same name. (the name node).

I then use this PHP code to replace the node names

 <?php 
/**
 * @param $xml string Your XML
 * @param $old string Name of the old tag
 * @param $new string Name of the new tag
 * @return string New XML
 */
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;
}

foreach ($toRemove as $node)
{
    $node->parentNode->removeChild($node);
}

return $dom->saveXML();
}

  // Load XML from file data.xml
$xml = file_get_contents('data.xml');

$xml = renameTags($xml, 'name', 'newName');

echo $xml;
?> 

This function replaces all of the name nodes with newName; however, I want to only replace one instance of the name tag because I want to rename each of the name tags.
If I call another
$xml = renameTags($xml, ‘name’, ‘newName2’);

It wont work, it will only use the first instance of $xml.

Any Idea how I can change my code to allow me to replace each name node individually?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:18:03+00:00Added an answer on May 27, 2026 at 11:18 am

    If you simply want to rename the first node you encounter, add a break; statement at the end of your first foreach loop like this first example. However, this is horribly inefficient and a better way to do it is demonstrated in the second (CORRECT) example at the bottom.

    THE WRONG WAY
    Everytime you replace XML like this an angel sheds a tear …

    $src = "
    <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>
    </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($src, 'name', 'newName');
    echo $xml;
    

    THE CORRECT WAY

    function renameTags($xml, $old, $new)
    {
      $dom = new DOMDocument();
      $dom->loadXML($xml);
    
      // find the first node with the specified tag name
      $oldNode = $dom->getElementsByTagName($old)->item(0);
    
      // clone the node (deep copy)
      $doppelganger = $oldNode->cloneNode(TRUE);
    
      // import our cloned node to this dom document
      $doppelganger = $dom->importNode($doppelganger, true);
    
      // Create new node with the value from the copied node
      $newNode = $dom->createElement($new, $doppelganger->nodeValue);
    
      // update all the attributes of the new node with those from the copy
      foreach ($doppelganger->attributes as $attrName => $attrNode) {
        $newNode->setAttribute($attrName, $attrNode);
      }
    
      // append the newNode copy to the dom
      $oldNode->parentNode->appendChild($newNode);
    
      // remove the old node
      $oldNode->parentNode->removeChild($oldNode);
    
      $dom->formatOutput = TRUE;
      return $dom->saveXML();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an XML file that has a number of nodes, each of which
I currently have an iPhone app that reads data from an external XML file
I have an XML file that I would like to map some attributes of
I have an XML file with the following structure: <Products> <Product name=MyProduct1> <Components> <Component
I have an xml file that has a start date and length of a
I have an XML file that encodes a directed acyclic graph (DAG) that represents
I have an XML file that is very long, but here is a shot
I have an XML file that lists a series of items, and the items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.