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 6143789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:33:57+00:00 2026-05-23T18:33:57+00:00

The problem seems straightforward, but I’m having trouble getting access to the tag name

  • 0

The problem seems straightforward, but I’m having trouble getting access to the tag name of a SimpleXMLElement.

Let’s say I have the follow XML structure:

<xml>
     <oldName>Stuff</oldName>
</xml>

And I want it to look like this:

<xml>
     <newName>Stuff</newName>
</xml>

Is this possible to do without doing a copy of the entire object?

I’ve started to realize the errors of the ways I am approaching this problem. It seems that I need to convert my SimpleXMLElement into a DOM object. Upon doing so I find it very hard to manipulate the object in the way I want (apparently renaming tags in a DOM isn’t easy to do for a reason).

So… I am able to import my SimpleXMLElement into a DOM object with the import, but I am finding it difficult to do the clone.

Is the following the right thinking behind cloning a DOM object or am I still way off:

$old = $dom->getElementsByTagName('old')->item(0); // The tag is unique in my case
$new = $dom->createElement('new');

/* ... some recursive logic to copy attributes and children of the $old node ... */

$old->ownerDocument->appendChild($new);
$new->ownerDocument->removeChild($old);
  • 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-23T18:33:58+00:00Added an answer on May 23, 2026 at 6:33 pm

    Here’s what’s probably the simplest way to copy a node’s children and attributes without using XSLT:

    function clonishNode(DOMNode $oldNode, $newName, $newNS = null)
    {
        if (isset($newNS))
        {
            $newNode = $oldNode->ownerDocument->createElementNS($newNS, $newName);
        }
        else
        {
            $newNode = $oldNode->ownerDocument->createElement($newName);
        }
    
        foreach ($oldNode->attributes as $attr)
        {
            $newNode->appendChild($attr->cloneNode());
        }
    
        foreach ($oldNode->childNodes as $child)
        {
            $newNode->appendChild($child->cloneNode(true));
        }
    
        $oldNode->parentNode->replaceChild($newNode, $oldNode);
    }
    

    Which you can use this way:

    $dom = new DOMDocument;
    $dom->loadXML('<foo><bar x="1" y="2">x<baz/>y<quux/>z</bar></foo>');
    
    $oldNode = $dom->getElementsByTagName('bar')->item(0);
    clonishNode($oldNode, 'BXR');
    
    // Same but with a new namespace
    //clonishNode($oldNode, 'newns:BXR', 'http://newns');
    
    die($dom->saveXML());
    

    It will replace the old node with a clone with a new name.

    Attention though, this is a copy of the original node’s content. If you had any variable pointing to the old nodes, they are now invalid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a straightforward task, but I'm having trouble figuring out the rails
This seems too straightforward as first to be a problem - but here is
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
My problem seems to be quite simple, but it's not working the intuitive way.
The problem seems very strange. I have a AJAX helper function within a same
I have a problem that seems like its a result of a deadlock-situation. Whe
Seems like a simple problem: I have an SVN repo inside our firewall. I
This seems to be a common problem but I cannot find a solution. I
I'm a PHP dev and I'm new to Rails but have been getting on
It seems that similar questions have been asked, but they don't quite fit what

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.