Jump to content

Xml User Database | Setting Attributes


mobone
 Share

Recommended Posts

I joined to ask how to set attributes in a xml file using simplexml in php. Not add them but change the values that are already there. I believe there is a way in DOM but I am using simplexml right now.I'm really new at this and am really confused with how this all works but trying to get it down. How should my user database be set up? i was using attributes like:<user name="frank" password="anything"><stuff /></user>But now I'm thinking of changing it to:<user><frank><password>anything</password><stuff /></frank></user>Then I can use "$xml->users->frank->stuff = $more_stuff" to easily change the values as I have given up on changing the attributes now.Or should I make a new xml file for each user? I know for sure I wont have more than 1000k users.Does anyone have any really good simplexml resources besides the short and overly-simple php page on it.Should I just give up on simplexml and learn DOM instead??Thanks for any help. Sorry there isn't any indents, I tried.

Edited by Mobone
Link to comment
Share on other sites

I'm not real great with XML, and I'll try to answer your question if I don't see others doing it, but one thing I'll say is not to use a value as an element name. It's going to be complicated if you name an element with the user's name. It would be better to have a username element and give the name there. e.g.:

<user>  <username>frank</username>  <password>anything</password>  <stuff /></user>

or:

<user name="frank">  <password>anything</password>  <stuff /></user>

For indenting put everything in a code block, the word "code" in square brackets without spaces. Maybe this will print:[ code][/code]That, minus the space. If you want a scrollbar, use codebox instead.

Edited by justsomeguy
Link to comment
Share on other sites

How would I go through and find Frank and then change his password to a different one.I thought it would beuser->[username = frank]->password = $new_pass;but its not, thats an xpath syntax.The only examples I've seen is:user->username[2]->password = $new_pass;But I dont understand how I know that Frank is in fact the 3rd person.

I'm not real great with XML, and I'll try to answer your question if I don't see others doing it, but one thing I'll say is not to use a value as an element name. It's going to be complicated if you name an element with the user's name. It would be better to have a username element and give the name there. e.g.:
<user>  <username>frank</username>  <password>anything</password>  <stuff /></user>

or:

<user name="frank">  <password>anything</password>  <stuff /></user>

For indenting put everything in a code block, the word "code" in square brackets without spaces. Maybe this will print:[ code][/code]That, minus the space. If you want a scrollbar, use codebox instead.

Edited by Mobone
Link to comment
Share on other sites

I don't understand this XML stuff at all... how would I find frank, and change his password using SimpleXML. Thanks.

<?xml version="1.0"?><members>	<user>		<name>frank</name>		<password>Any</password>	</user></members>

Edited by Mobone
Link to comment
Share on other sites

If you're comfy with XPath, use XPath:

$matches = $xml->xpath('/members/user[name="frank"]');$matches[0]->password = 'Whatever you want the new password to be';

Personally, I don't like SimpleXML, so if there's a way to do that without XPath, I don't know it. There's sure a way with DOM though. Still, in both cases (DOM and SimpleXML), using XPath to find the right node is the simplest way.One more thing... even though I'm known as an XML advocate around here, even I wouldn't use XML to store usernames and passwords. Especially in your case where you expect many users. Consider using a database for that, or if you must do it without a database - split the information about each user to a separate file. Ideally, if possible, use XMLReader instead of SimpleXML or DOM to read the data, in order to keep the memory low.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...