note 127996 added to simplexmlelement.addattribute

From: Date: Fri, 09 Dec 2022 10:25:30 +0000
Subject: note 127996 added to simplexmlelement.addattribute
Groups: php.notes 
Request: Send a blank email to php-notes+get-219952@lists.php.net to get a copy of this message
I needed a way to update an attribute, but I could not find any documented way to do this. TL;DR: It's not only possible, but unbelievably simple. See the other note I submitted for what actually works (sorry, I could not fit it all in a single note due to the 4095 character limit). After much experimentation, here's my findings of what does NOT work; hopefully, it saves you the time and frustration it cost me. Given the following example: <?php $xml = <<<EOF <?xml version="1.0" encoding="utf-8" ?> <Animals> <Dog/> </Animals> EOF; $animals = new SimpleXMLElement($xml); echo $animals->Dog->asXML(); // <Dog/> ?> Test #1 - attempt to call addAttribute() twice TL;DR: PHP Warning: SimpleXMLElement::addAttribute(): Attribute already exists <?php ... $animals->Dog->addAttribute('breed', "chihuahua"); echo $animals->Dog->asXML(); // <Dog breed="chihuahua"/> $animals->Dog->addAttribute('breed', "poodle"); // PHP Warning: SimpleXMLElement::addAttribute(): Attribute already exists echo $animals->Dog->asXML(); // <Dog breed="chihuahua"/> ?> Test #2 - attempt to iterate attributes() (an Iterator) by reference TL;DR: An iterator cannot be used with foreach by reference <?php ... $animals->Dog->addAttribute('breed', "chihuahua"); echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> try { foreach ($animals->Dog->attributes() as $attr => &$value) { if ("breed" === $attr) { $value = "poodle"; } } } catch (Throwable $e) { echo $e->getMessage(); // An iterator cannot be used with foreach by reference } echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> ?> Test #3 - attempt to re-parse child element and then call addAttribute() on the newly parsed element, as suggested in a note I found here TL;DR: PHP Warning: SimpleXMLElement::addAttribute(): Attribute already exists <?php ... $animals->Dog->addAttribute('breed', "chihuahua"); echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> $animals->Dog = new SimpleXMLElement($animals->asXML()); $animals->Dog->addAttribute('breed', "poodle"); // PHP Warning: SimpleXMLElement::addAttribute(): Attribute already exists echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> ?> Test #4 - attempt to modify @attributes array element TL;DR: PHP Notice: Indirect modification of overloaded element of SimpleXMLElement has no effect <?php ... $animals->Dog->addAttribute('breed', "chihuahua"); echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> $dogAttrs = $animals->Dog->attributes(); // object(SimpleXMLElement)#4 (1) { // ["@attributes"]=> // array(1) { // ["breed"]=> // string(9) "chihuahua" // } // } $dogAttrs['@attributes']['breed'] = "poodle"; // PHP Notice: Indirect modification of overloaded element of SimpleXMLElement has no effect echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> ?> Test #5 - cast attributes() to array and set value of 'breed' element TL;DR: does not update value in original object <?php ... $animals->Dog->addAttribute('breed', "chihuahua"); echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> $dogAttrs = (array) $animals->Dog->attributes(); // array(1) { // ["@attributes"]=> // array(1) { // ["breed"]=> // string(9) "chihuahua" // } // } $dogAttrs['@attributes']['breed'] = "poodle"; echo $animals->Dog->asXML(); // <Dog breed="chihuahua"> ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.175 (proxied: 144.129.235.105) ---- Manual Page -- https://php.net/manual/en/simplexmlelement.addattribute.php Edit -- https://main.php.net/note/edit/127996 Del: integrated -- https://main.php.net/note/delete/127996/integrated Del: useless -- https://main.php.net/note/delete/127996/useless Del: bad code -- https://main.php.net/note/delete/127996/bad+code Del: spam -- https://main.php.net/note/delete/127996/spam Del: non-english -- https://main.php.net/note/delete/127996/non-english Del: in docs -- https://main.php.net/note/delete/127996/in+docs Del: other reasons-- https://main.php.net/note/delete/127996 Reject -- https://main.php.net/note/reject/127996 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219952) next »