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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…