XML Tutorial : Loading XML in PHP


Join us as we learn how to work with XML. Starting with an xml tutorial that covers how xml works, how to integrate it into JavaScript, E4X and even how to work with it in PHP. For the full course please visit:

So why use a database if we have XML?
Before we can delve into why to use one over the other, let’s take a quick detour and understand what databases really are. A database goal is to store information in a way that’s easy to search. It’s built for a large amount of data that changes quickly and repeatedly. Imagine a web site working off an XML file that changes often! Imagine 10 people trying to change the file at the same time. What would happen? Something very unexpected, for sure. On the other hand, databases are built to deal with these types of issues: to read, write and produce a quick way to get only the data needed.

XML is really what you see is what you get. Now we must mention that you can produce XML on the fly on the server to transfer data (many times these types of data are called Data Packets or Deltas.) But that’s just cheating, creating XML on the fly (not exactly cheating…) it creates many more steps for our poor server than we would need to grab data from a DB, convert it to XML, and then send it to the client. Whenever we can skip steps, it’s always better and faster: that leads us to the next modern type, JSON.

What is JSON and when to use it (vs. XML)?
JSON is one of the more modern options. It was born out of some issues with XML when the need for smaller data packets arose when dealing with live data such as live games chat and such. The core idea behind JSON that separates it from XML is its goal to keep the data size small to enable quicker communication. This technology is ideal in scenarios when you have a lot of data communication and you want to save on data sizes.

XML can easily become huge as it has a lot of structure involved. JSON on the other hand is a simple translation of objects into strings. You don’t need to actually know how to write or read JSON: all you need is a parser that will take your objects, convert them to a JSON string, and send them out. On the other end you would need a JSON encoder. Most modern (if not all) languages have a built-in JSON encoder/decoder or an open source solution (including XML, so it’s down to needs.) JSON is ideal for fast small data feeds: it isn’t optimized for large amounts of data or ideal for storing as it’s not very human-readable (it can be and is stored many times but it really shines in small live data interactions). So JSON would be ideal for data that needs to be as small as possible and not saved, while XML is ideal for storing large amounts of data that might need to be human-readable (so you can manually edit the data.)