I'm working on a simple homework question, which requires me to store details of a user (such as username, password etc.) in an XML file, and use this file for site login purposes and also appending this XML file on new user registration (ie. updating the xml file with username and password details of the new user). This is the structure of my xml file (please pardon me if the structure is poor, I've made too much progress to change the stucture now!):
<?xml version="1.0" standalone="yes"?> <users> <user> <username>cookie_monster</username> <password>cookiesrock</password> </user> <user> <username>homer_simpson</username> <password>duffbeer</password> </user> ... ... </users>
Now, on the registration.php page, I use the code (snippet) below for adding information about the new user (after proper validation, of course, but for the sake of clarity I'm not including the validation):
$xml = simplexml_load_file('users.xml'); $xml->addChild("user", ""); //add <user> </user> to the xml file $count = count($xml->user); //counts the number of <user> </user> elements in the xml file $xml->user[$count]->addChild("username", $user); //add <username> </username> inside <user> </user> $xml->user[$count]->addChild("password", $pass); $xml->asXML('users.xml'); echo "<h3>Registration Success!</h3>";
When I execute the script, I get this error: Fatal error: Call to a member function addChild() on a non-object in (*file path*) on line 138, where line 138 is $xml->user[$count]->addChild("username", $user);
Am I doing something wrong here? I have been breaking my head over this for over an hour with no luck!
Also, is there an "editChild()" or similar function in simpleXML which I could use to edit certain elements, for eg. when a user wants to change his/her password? Or must I absolutely necessarily use DOM for this purpose?