PHP :: JSON vs XML

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

PHP :: JSON vs XML

Post 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?
Last edited by kendall on Mon Jun 23, 2008 3:23 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP :: JSON vs XML

Post 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.
(#10850)
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: PHP :: JSON vs XML

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: PHP :: JSON vs XML

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: PHP :: JSON vs XML

Post 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!
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: PHP :: JSON vs XML

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: PHP :: JSON vs XML

Post 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:

Code: Select all

"bold" => true,
"name" => "jack"
In JSON this would be:

Code: Select all

"bold" : true,
"name" : "jack"
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:

Code: Select all

"bold" => true,
"name" => "jack"
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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP :: JSON vs XML

Post 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.
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: PHP :: JSON vs XML

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP :: JSON vs XML

Post by Christopher »

That's what XML is designed to handle -- complexity. ;)
(#10850)
Post Reply