A Couple of simpleXML questions.

I'm having trouble with addChild() and query about editing an exis

Page 1 of 1

1 Replies - 3572 Views - Last Post: 16 August 2008 - 08:11 AM Rate Topic: -----

#1 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

A Couple of simpleXML questions.

Posted 16 August 2008 - 04:12 AM

Hi there,

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"?>





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);
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? :(

Is This A Good Question/Topic? 0
  • +

Replies To: A Couple of simpleXML questions.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: A Couple of simpleXML questions.

Posted 16 August 2008 - 08:11 AM

You have to use addChild on the <users> element. When you use an array with $count, you are actually trying to access an element that is not there. Remember that arrays start at 0 so $count - 1 is going to be your last element. BUT... if you are just attempting to add a new user, you have to do an addChild on the <users> element since that is the parent.

As for your question of editing, you can use your $xml->user[$count]->username = "new username value"; to modify that element.

So in short...

1) Do addChild on the <users> root element to add another user
2) Your array is attempting to access an element that is one subscript bigger than the size of the array. Arrays start at zero.
3) You can change values by using your user array with the elements name to edit its value.

Hope this helps you out. :)

"At DIC we be Simple XML handling code ninjas... heck, to us everything is simple." :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1