XML & PHP

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

XML & PHP

Post by Torniquet »

ok as i creep closer to redoing my map section of my game. and im thinkin about further game play. i have thought about using XML to hold information about maps etc.

my question here is...

how would i go about editing a section of the XML document?

for eg...

Code: Select all

<map>
    <tile>
       <x_coord>3</x_coord>
       <y_coord>4</y_coord>
       <type>grass</type>
   </tile>
</map>
now say if i wanted to change the value of the type child from grass to concrete.

how would i go about doing that via php?
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: XML & PHP

Post by Jackolantern »

I would suggest using the SimpleXML for XML manipulation. I am not extremely familiar with it myself, but in PHP using a library to manipulate XML is a must, since parsing large text files manually is far too slow for such a core aspect of a game as a map system.

While I don't know exactly how you would do that with SimpleXML, it child value manipulation is a very basic function so it is likely one of the first couple of things you would learn in the SimpleXML section of the PHP manual.

EDIT: For a fairly simple map XML system, however, I would have to think it may be better off in the database. If you store the XML in the database, you will just be adding another layer of translation to each access, and if you keep them as text files, you will be relying on rapidly opening and closing many files in the file system, which can begin to bog down any OS. XML is not at its most useful for simply storing sets of data like:

Code: Select all

<map>
    <tile>
       <x_coord>3</x_coord>
       <y_coord>4</y_coord>
       <type>grass</type>
   </tile>
</map>
While I realize this may just be an example and your actual schema may be far more complex, XML is really used at its best to represent interaction of data within their hierarchy (ie, when the data client must have the hierarchy along with hierarchy attributes to properly process it) and of course for data files that are going to be open and usable by other systems (which your maps probably won't be). Otherwise, "list encapsulation" styles of XML that can easily be represented by columns and rows in the database are better to be held there without XML. RDBs are optimized and optimized and optimized again for lightning fast threaded data access and it is hard to beat that with either the file system (XML files) or by adding more work for PHP to do (XML in the database). You would need to ensure you have a specific need for XML before you make it a core aspect of your design.
The indelible lord of tl;dr
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: XML & PHP

Post by Torniquet »

yeah i have looked into the simple xml library. and yes wht you see is an example.

i was going to try and use it to temprarily store instances of player locations within a dungeon.

my plan is to keep my current map system for browsing about towns, and have dungeon P&P style fields. and run the temporary player positioning from an xml file.

that way the plan was to give each one an ID which can be accessed when playing in a dungeon, and also allow if for multiple user play. rather than keep reading and writing to the DB all the time.

dunno was just a thought, if using a DB to handle this data would be easier then ill prob go with that :)

thats if my half assed description of my plan made any sense wot so ever lol (Y)
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: XML & PHP

Post by Jackolantern »

If it is just a DB vs. XML file decision and either can represent the data easily with little additional structural data, I would probably give the nod to the database. The bottleneck in the XML file situation would be the repeated opening, closing and possibly saving of text files. RDBs live just for that purpose, whereas speed of opening and closing is probably not on the top 5 list of any major OS.

However, you likely would not see much difference until you got into decently high populations of players, when speed of data access becomes crucial. And there may be some benefit to having it in XML format for unforeseen future events that would allow other software to slog through your map and other data.

What you could do is create a fairly simple PHP page that can perform an XML dump of your database, and maybe try it out both ways at some point. You could create some benchmarking scripts that print the time in ms, run a bunch of data access routines, and then print the milliseconds out again. I actually bet a lot of people around here would be interested in the results as well :)
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: XML & PHP

Post by Chris »

I'd go with using JSON. Make PHP write to a file with JSON and let Ajax reload it. That way you won't need any parsing in JavaScript. Only in PHP.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: XML & PHP

Post by Torniquet »

whenever ive looked for decent json tuts, i couldnt find non :s anyone know of any?
New Site Coming Soon! Stay tuned :D
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: XML & PHP

Post by Chris »

It's very easy.. if you know how to write an object in JavaScript you'll know how JSON works..

Here's what an array would look like in PHP:

Code: Select all

$array = array (
    'a' => 'apple',
    'b' => 'banana',
    'c' => array (
            0 => 'x'
            1 => 'y'
            2 => 'z' ) );
 
This is what it would look like in JavaScript:

Code: Select all

array = [
    'a' : 'apple',
    'b' : 'banana',
    'c' : [
          0 : 'x',
          1 : 'y',
          2 : 'z' ]]
And this is what it would look like in JSON

Code: Select all

{ 'a' : 'apple', 'b' : 'banana', 'c' : [ '0' : 'x', '1' : 'y', '2' : 'z'  ] }
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: XML & PHP

Post by Torniquet »

thanks chris.

ill have another go at seein if i can find anything thtll teach me more about it out there lol.
New Site Coming Soon! Stay tuned :D
Post Reply

Return to “Coding”