0

With SimpleXML, it is possible to add/change/delete attributes of a selected Node «on the fly» by typing e.g.

$child->addAttribute('n', $occ_order);

is there a way to alter the name of the $child element as well? I would expect something like

$child->setName('newTagName');

but I cannot find a corresponding function in the API.

Thanks in advance for your hints!

sam
  • 1,406
  • 2
  • 15
  • 25
  • possible duplicate of [How do you rename a tag in SimpleXML through a DOM object?](http://stackoverflow.com/questions/6694956/how-do-you-rename-a-tag-in-simplexml-through-a-dom-object) – Gordon Sep 14 '11 at 12:07

2 Answers2

1

You can remove it with unset() if you know exactly where it is i.e. $main->target would be removed as unset($main->target) and add it again with addchild() i.e. $main->addchild(name, value).

Griwes
  • 8,805
  • 2
  • 43
  • 70
user1123382
  • 120
  • 6
1

From the docs[1] it looks like you can't. And it would be kind of weird if you could too - changing the name makes it a different/new element. What you want to do is delete that element and add a new one in the same place. However whilst there is a addChild method it doesn't look like there is a delete method. So maybe simpleXML is not the right tool here.

Edit: Indeed simpleXML does not provide a delete method. For info on how to do this, please see this answer[2].

1 http://php.net/manual/en/book.simplexml.php

2 Remove a child with a specific attribute, in SimpleXML for PHP

Community
  • 1
  • 1
Richard H
  • 38,037
  • 37
  • 111
  • 138