note 103865 added to simplexmlelement.addattribute

From: Date: Sat, 07 May 2011 14:49:11 +0000
Subject: note 103865 added to simplexmlelement.addattribute
Groups: php.notes 
Request: Send a blank email to php-notes+get-179525@lists.php.net to get a copy of this message
You can access a node child with his name this way : <?php $root = new SimpleXMLElement($filePath); echo $root->nodeName->attributes; // It works recursivly so this will work too echo $root->nodeName->subNodeName->attributes(); ?> But if you want to ADD an attribute to a children you MUST use the children() method to access and modfiy it or it will modify the parent's attributes. <?php // Add a subnode $element->nodeName->addChild('subNodeName', "whatever you want"); // Get the pos in the childrens of the parent node $lastNodePos = $element->nodeName->count()-1; // Get the parents node childrens $nodeChildrens = $element->nodeName->children(); // Add an attribute to the last created $nodeChildrens[$lastNodePos]->addAttribute('attributeName', "The attribute value); ?> ---- Server IP: 194.153.94.10 Probable Submitter: 85.5.114.25 ---- Manual Page -- http://www.php.net/manual/en/simplexmlelement.addattribute.php Edit -- https://master.php.net/note/edit/103865 Del: integrated -- https://master.php.net/note/delete/103865/integrated Del: useless -- https://master.php.net/note/delete/103865/useless Del: bad code -- https://master.php.net/note/delete/103865/bad+code Del: spam -- https://master.php.net/note/delete/103865/spam Del: non-english -- https://master.php.net/note/delete/103865/non-english Del: in docs -- https://master.php.net/note/delete/103865/in+docs Del: other reasons-- https://master.php.net/note/delete/103865 Reject -- https://master.php.net/note/reject/103865 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179525) next »