I have xml files, which looks like this:
Expand|Select|Wrap|Line Numbers
- <toplevel>
- <username id ="Jonh">
- <playersname name ="Kostik">
- <starcolor>3</starcolor>
- <levels>19</levels>
- </playersname>
- <playersname name ="Mostik">
- <starcolor>1</starcolor>
- <levels>19</levels>
- </playersname>
- </username>
- </toplevel>
What I'm trying to do:
Php receives parameters. He loads user.xml and checks. If Username already present, he checks all playersname for that users. If playersname present, scipt updates starcolor and lessons, and writes it to the file.
If User present, but players is not, script crates new nodes for new players. If users is not present, script creates new user with players nodes near end of the file. I know, i is probably very hard and large script, so I will aprreciate any help and pointers. Here is what I made so far, it is half finished:
[PHP]<?php
$UsersName=$_POST['UsersName'];
$index = 0;
$PlayersName=$_POST['PlayersName'];
$StarsColor=$_POST['StarsColor'];
$SLessonsComplete=$_POST['LessonsComplete'];
$dom=new DOMDocument();
$dom->load('users.xml');
$dom->formatOutput = true;
echo $dom->saveXML(); // show before file
$allnodes = $dom->getElementsByTagName('username');
foreach ($allnodes as $node)
{
if ($node->nodeName=='username' and $node->getAttribute('id')==UsersName)
{
$allplayers = $dom->getElementsByTagName('playersname');
foreach ($allplayers as $player)
{
if ($player->nodeName=='playersname' and $node->getAttribute('id')==PlayersName)
{
x=xmlDoc.getElementsByTagName("topleve;")[index];
y=x.childNodes[0];
}
$val=$node->getAttribute('value') . ' updated';
$node->setAttribute ( 'value', $val );
}
index++;
}
echo $dom->saveXML(); //show after file
$dom->save('users.xml');
exit;
?>
[/PHP]