What's better: Json or XML (PHP)

22,030

Solution 1

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.

Solution 2

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

Solution 3

I feel the following StackOverflow entries answer your question best:

As for which one is easiest?
jQuery makes serializing your JavaScript objects into JSON objects easy.

But, if you're still unsure...

Here are some other articles:

Solution 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.

Solution 5

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.

Share:
22,030
Artem Shmatkov
Author by

Artem Shmatkov

Getting into Rust these days

Updated on July 03, 2020

Comments

  • Artem Shmatkov
    Artem Shmatkov almost 4 years

    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.