-1

I want to create PHP page that allow me to add data into the XML file, I want to make the xml file looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfPlayer>
  <Player>
    <DisplayName>FuznesS</DisplayName>
    <UserID>123123</UserID>
  </Player>
   <Player>
    <DisplayName>Vortex</DisplayName>
    <UserID>321321</UserID>
  </Player>
</ArrayOfPlayer>

and be able to add more

<Player>
    <DisplayName>Vortex</DisplayName>
    <UserID>321321</UserID>
</Player>

and everyting that I adding will be inside the

<ArrayOfPlayer>

</ArrayOfPlayer>

thanks for helping.

1 Answers1

0

How about this, just replace the variables:

<?php
$xml = new SimpleXMLElement('<ArrayOfPlayer/>');

foreach ($players AS $player) {
    $track = $xml->addChild('Player');
    $track->addChild('DisplayName', $display_name);
    $track->addChild('UserID', $user_id);
 }

 Header('Content-type: text/xml');
 print($xml->asXML());

* EDIT *

Well, here's my revised answer, hope this will help:

<?php
$filename = 'file.xml';
$new_displayname = 'Vortex';
$new_userid = '321321';


$doc=new DOMDocument();
$doc->formatOutput = true;
$doc->load($filename);
$root=$doc->documentElement;

$arrayofplayer = $doc->getElementsByTagName("ArrayOfPlayer");

$arrayofplayer = $doc->createElement('Player');
$arrayofplayer = $root->appendChild($arrayofplayer);

$player = $doc->createElement('DisplayName');
$player = $arrayofplayer->appendChild($player);
$displayname = $doc->createTextNode($new_displayname);
$displayname = $player->appendChild($displayname);

$player = $doc->createElement('UserID');
$player = $arrayofplayer->appendChild($player);
$userid = $doc->createTextNode($new_userid);
$userid = $player->appendChild($userid);

$doc->save($filename);
?>
Wietse
  • 372
  • 7
  • 18
  • Parse error: syntax error, unexpected 'AS' (T_AS), expecting ';' in C:\wamp\www\test.php on line 6 ; LINE 6 for ($players AS $player) { – user2852249 Jan 30 '14 at 11:39
  • `for ($players AS $player) {` should be `foreach ($players as $player) {` – stckrboy Jan 30 '14 at 11:55
  • now its giving me the following error ; This page contains the following errors: error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error. – user2852249 Jan 30 '14 at 11:56
  • I think the code was meant as a starter - you can use the loop if you want to add multiple users. Remove the foreach line and the closing bracket. Remember that you'll still need to set values for `$display_name` and `$user_id` – stckrboy Jan 30 '14 at 12:06
  • Yes, it's a starter. Create an array of players (either from a database or manually) or loop them. – Wietse Jan 30 '14 at 12:09
  • I already set values, and when I'm doing the thing that you saying, its not saving the file, and overwrite the existing data, and I'm new with php, and especially with XML, I tried to make it work all yesterday night, and just failed. – user2852249 Jan 30 '14 at 12:13
  • How did you set the values? – Wietse Jan 30 '14 at 12:35
  • I made index page with forms I entered the username and userID there and action takes it to test.php *the code you gave me* and then its suppose the enter the data into the xml file. but for me its just overwrite the exist data or making everytime a new for each . – user2852249 Jan 30 '14 at 12:39
  • ah, I see... and the xml file needs to be static then and updated with the values from the form? Is that what you want it to do? – Wietse Jan 30 '14 at 13:49
  • Yes, this is what I want. – user2852249 Jan 30 '14 at 14:23
  • Please see my revised answer... – Wietse Jan 30 '14 at 15:28
  • Thank you very much Wietse, you helped me alot, I'll check it more few minutes. – user2852249 Jan 30 '14 at 15:54
  • Its making this error now ; Fatal error: Call to a member function appendChild() on a non-object in C:\wamp\www\test.php on line 15 ; line 15 - $arrayofplayer = $root->appendChild($arrayofplayer); – user2852249 Jan 30 '14 at 16:12
  • What PHP version are you using? You need PHP5 for this... – Wietse Jan 30 '14 at 16:30
  • I'm using the newest version of wampserver, its PHP 5.4 – user2852249 Jan 30 '14 at 16:35
  • there is any solution for this problem? – user2852249 Jan 30 '14 at 17:06
  • I saved your xml file as file.xml, then saved my php-script as index.php, and pointed my browser there. And this works for me! – Wietse Jan 31 '14 at 08:20
  • Thank you very much, its working perfectly, I just forget the xml file empty, so its made this error, but now everything working great. Thank you very much :). – user2852249 Jan 31 '14 at 08:33