Alex Rivera | Logout

What's better: Json or XML (PHP)

Asked 2009-07-12T20:19:01.373
31

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.

Edit
Report

2 Answers

4

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.

answered 2009-07-12T20:45:43.613
0

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.

answered 2009-07-12T20:28:16.830

Your Answer