tags:

views:

1681

answers:

10

I'm manipulating few data with PHP, I have the choice between Json and XML. But I don't know what to choose, I never worked with one of them. So I want the easiest one.

Also I wonder if there's good classes that can make parsing (XML or Json) easier.

I focus mainly on ease of use rather than speed and scale.

+1  A: 

If you choose XML use SimpleXML, an extremely simple to use library

Andrew G. Johnson
+20  A: 

json.org makes JSON parsing simple. SimpleXML makes XML simple for PHP.

When choosing which one, it really depends on your application. If you will be passing data to JavaScript for processing on the client-side, then I recommend JSON. IMO, XML is better for data interchange between systems because it is very well understood and human readable (making interpreting easier for you). JSON is smaller over the wire, as there are no closing tags.

geowa4
same as I said. +1 for "it really depends"
Martin K.
I agree with "it depends", but I don't think xml has any edge wrt readability compared to json; they are roughly similar. JSON size benefit exists, but usually doesn't matter all that much: if size does matter, gzipping data makes them about same size (same information content etc).Personally I would choose based on tools (ease-of-use), but can't comment wrt PHP. For Java there are lots of tools for both, I assume same is true for PHP too.
StaxMan
+5  A: 

JSON parsing and generating is natively included in php: http://www.php.net/json

Dykam
+1  A: 

If you've not used either, then go for JSON. The answers so far are correct in that there is a lot of help for both and you'll get up and running on either fairly quickly. JSON has the added advantage of being less bloated than XML and can easily be 'translated' to javascript when on the client

David Archer
A: 

There are a number of (perhaps one-sided) JSON vs XML articles at the bottom of the json.org page.

Here's one.

I generally prefer JSON because it's much better suited for data I typically would represent, which in my line of work would be things like vertices, meshes, etc. Depending on your data XML might be a fine format.

Dan Olson
A: 

I use JSON with PHP because of how simple it is:

$arr = array('foo' => 'bar', 'animals' => array('cat', 'dog')); // Create any array.
$json = json_encode($arr); // Converts any array to JSON.
James Skidmore
can't you convert an array to XML??
Omar Abid
totally. xml_encode($var, false, "foo");where $var is the array, false to not display id refs, and "foo" is the name of the XML Root. http://www.projectzero.org/sMash/1.0.x/docs/apidocs/PHP/xapidoc/XMLExtension/xml_encode.html
Yar
+2  A: 

One thing to consider is that, at least on download, the difference in size between XML and JSON (XML being bigger) is not really interesting because you will using GZIP compression over HTTP.

If you are sure that you will working with Javascript and never have any other type of client (another website, a Flash/Flex client, whatever) then JSON is great and perhaps a bit less painless. On the other hand, doing XML now saves you time should you ever need to get stuff working over XML.

Anyway, a good thing to do is check what the Google guys are doing using LiveHTTPHeaders for Gmail or any of their apps. I'm pretty sure that it's XML and not JSON, but you can check me on that. In all cases, doing what Google does might be considered to be "not thinking," but they're generally smarter than me and have many more person-hours to think anyway :)

Yar
the difference in size ... is not really interesting << It's processing overhead. For big files (x Gb) it can be also an issue.I've seen a lots of "design by contract" google XML apps. I prefer XML.
Martin K.
Hmmm... yeah, if we're talking about huge files, the world becomes plainly empirical: you have to test and benchmark and get a clue, I think. JSON is obviously faster than demarshalling from XML... isn't it?
Yar
""doing what Google does might be considered to be "not thinking," but they're generally smarter than me and have many more person-hours to think anyway :)""not trueIt'll depend on many things, they don't choose the "BEST", they choose the "BEST ONE FOR GMAIL"
Omar Abid
+2  A: 

It depends.

Example: You want to place your application into a big context (EAI), then you should use XML (maybe Webservices & WSDL).

Example2: If you want a simple user interface, based on javascript and ajax you should consider JSON.

JSON is most useful when you work with javascript. In the most cases XML libraries are so easy to use, that the ease of use is nearly the same.

Martin K.
+3  A: 

JSON views your data as structures known from programming languages: maps/dictionaries (called 'objects' in JSON), arrays and literals. JSON puts these basic constructs into hierarchies.

XML is also hierarchical, but it has three basic parts: elements (with start/end tags), attributes within elements, and text content. Elements and text content can be mixed, so it is suited to marking up documents. JSON isn't very good for this.

XML has huge ecosystem built around it with lot of ready tools: various schemas to check that your XML documents are valid, transformation language, XPath for navigating your document, etc. JSON is lacking in this area too.

XML is much more complex than JSON though. It's easy and fun to write your own JSON parser, but writing XML parser is huge undertaking (surely not for somebody who is new to XML). In Javascript, JSON can be parsed by evaluating the text (this is pretty insecure though).

If you need to transfer data between two systems, JSON is fine for this. If you want to use more advanced properties from XML (entities, automatic inclusion of other documents, schema, implicit attribute values, etc.), or mix content and markup, XML will work better for you.

Peter Štibraný
Are there not libs for XML manipulations on both client (basically all clients) and server (PHP) sides?
Yar
Daniel, I am sure there are. My answer focused more on "JSON vs XML" than on libraries. I usually work with Java, and there are tons of libraries for XML in Java world. In PHP I used SimpleXML for my (simple) needs, and it just worked.
Peter Štibraný
Understood, Peter, I guess I was asking how that 4th paragraph worked its way into your answer. I'll upvote since, reading your answer a few times, I realized that you actually point out something important: XML has an entire world (ecosystem as you say): JSON seems to not have that at all. Not that the questioner cares... :)
Yar
A: 

The easiest one to start is XML. The main reason for this is not parsing (for there are very good libs for XML and JSON that do this for you, see other posts), but the understandability:

JSON works with a lot of different parentheses, when looking at your XML data you will probably see any errors faster. (Assuming you know e.g. HTML)

It is also possible (but optional) to create an XML schema, that makes verifying your data automatically easy. This can save you a lot of time afterwards!

Tarnschaf