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.
JSON parsing and generating is natively included in php:
I feel the following StackOverflow entries answer your question best:
As for which one is easiest?
But, if you’re still unsure…
Here are some other articles:
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.
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.
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.
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 🙂
Example: You want to place your application into a big context (EAI), then you should use XML (maybe Webservices & WSDL).
If you choose XML use SimpleXML, an extremely simple to use library
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.
There are a number of (perhaps one-sided) JSON vs XML articles at the bottom of the json.org page.
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.
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!