Page 1 of 1
PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 12:38 pm
by kendall
Hi guys,
need some advice on this. I have always been an XML fan. I think XML is simple and very human readable and I love its ability to be portable. But lately I have been heargin about JSON...Now i have never really looked at it but my questions comes from PHP's XML capabilities. i have been told that PHP's XML handling functions can be very resource consuming. making use of it for more light weight circumstances. But with this JSON syntax can the same effectiveness be achieved as the use of XML would (ajax/ data storage/ data transportation) and be more easy on system resources.
Have any of you guys used JSON in conjunction with PHP in terms of data storage and transportation?
What your opinion? JSON or XML
Why?
Re: PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 1:39 pm
by Christopher
The client Javascript code is very similar, so it really matters where the data comes from. If it is simply generated by a script that is called then JSON is simpler and more efficient in my opinion. If the use is more than just Javascipt consumption then XML makes more sense.
Re: PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 1:40 pm
by inghamn
It depends on the situation, really. If I'm providing data to be consumed by Javascript for some AJAX application, I'd provide the data in JSON. But if it's going from server to server, or being consumed by some other language, XML's the way to go. The self documenting aspect of well designed XML is the biggest reason I tend to favor XML.
Most of the time, though, I end up writing both output formats and just call for the data in whatever format's the easiest to parse in the client. Write both output formats and let the client decide.
Re: PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 2:45 pm
by Kieran Huggins
JSON is my weapon of choice - XML is a fine document structure, but JSON is soooo much easier to parse and generate that you'll never want to use XML for data exchange ever again. Smaller, too.
Another added benefit is that I can consume/generate it easily in everything I deal with: Javascript, PHP, Ruby. Also, writing a JSON parser/generator is completely trivial, even though they already exist for pretty much everything.
XML parsers are definitely a hog.
Re: PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 3:08 pm
by Ollie Saunders
I simply couldn't tolerate having to learn 8 different books on all the technologies surrounding XML (XML, SAX, DOM, Schema, XPath, XPointer, XSLT, XQuery) so that it can actually be used for stuff. It's good for document mark-up but for anything else it's a real hassle.
JSON FTW!
Re: PHP :: JSON vs XML
Posted: Mon Jun 23, 2008 4:21 pm
by inghamn
As a way to message format, XML works very well. Especially for unknown clients. For instance, we publish things like our locations of interest in the city as XML.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<locations>
<location id="9">
<name>Broadview Park</name>
<type>City Facility</type>
<address>704 W. Graham Dr.</address>
<description>704 W. Graham Drive, at the former Broadview School. Picnic shelter, accessible playground, basketball court, picnic tables & benches</description>
<latitude>39.140137</latitude>
<longitude>-86.541550</longitude>
</location>
<location id="10">
<name>Bryan Park</name>
<type>City Facility</type>
<address>1001 S. Henderson St.</address>
<description>Three softball fields, four basketball goals, three playgrounds, three picnic shelters, two volleyball courts, five tennis courts, three horseshoe pits, fitness area, restrooms, picnic tables, outdoor swimming pool</description>
<latitude>39.155499</latitude>
<longitude>-86.526413</longitude>
</location>
<location id="12">
<name>Building Trades Park</name>
<type>City Facility</type>
<address>619 W. Howe St.</address>
<description>Five basketball goals, two playgrounds, picnic shelter, play field, restrooms, picnic table</description>
<latitude>39.162285</latitude>
<longitude>-86.540672</longitude>
</location>
</locations>
The self-describing nature of XML is really nice if you want people to take your data and use it in unknown ways. Doesn't really require knowledge of anything except well-formed XML. (Although if you wanted to tell people what the tags were ahead of time, I guess you could send 'em an XML Schema, if you really wanted to)
Granted, I don't treat XML and JSON as serialization languages - I treat them as output formats. Which means someone marks up the XML and/or JSON by hand, just like they would an HTML view.
On the receiving end, PHP's simplexml makes reading XML data a breeze. If I'm consuming a feed using PHP, it's a whole lot easier to parse and do stuff with XML data, than with JSON.
It's only when I'm consuming the data using JavaScript that JSON comes in real handy.
Re: PHP :: JSON vs XML
Posted: Sun Mar 08, 2009 1:28 pm
by kaisellgren
Sorry if I raise too old thread, but I wanted to add something.
Let us say that we have an array having data:
In JSON this would be:
While in XML it would be:
Code: Select all
<bold>true</bold>
<name>jack</name>
If you look carefully, you should notice that when you decode back to PHP, the values will be:
JSON -> PHP:
XML -> PHP:
Code: Select all
"bold" => "true",
"name" => "jack"
As you can see, XML lacks of type hinting. Our boolean value became a string.
I just felt this is worth noting.
Re: PHP :: JSON vs XML
Posted: Sun Mar 08, 2009 2:38 pm
by Christopher
What about:
Code: Select all
<bold type="boolean">true</bold>
<name type="string">jack</name>
When it comes to descriptiveness and attributes it is pretty hard to compete with XML. Most other things need to resort to context.
Re: PHP :: JSON vs XML
Posted: Sun Mar 08, 2009 2:50 pm
by kaisellgren
arborint wrote:What about:
Code: Select all
<bold type="boolean">true</bold>
<name type="string">jack</name>
When it comes to descriptiveness and attributes it is pretty hard to compete with XML. Most other things need to resort to context.
That would do it. It's just, it gets more complex.
Re: PHP :: JSON vs XML
Posted: Sun Mar 08, 2009 6:53 pm
by Christopher
That's what XML is designed to handle -- complexity.
