This first article of a three-part series introduces PHP5’s XML implementation and help those relatively new to using XML with PHP to read, parse, and manipulate, and write a short and uncomplicated XML file using the DOM and SimpleXML in a PHP environment.
PHP seems to be going through a rough time at the moment, with more and more of it’s users defecting to languages like Ruby and Python. One positive thing about PHP5 is the new XML extension (mentioned in the article), and IMO it kicks the pants off anything I’ve seen in PHP’s competitor (Scripting) languages. As an example, I have a project which uses PHP5s custom stream handlers to read data out of a database during an XSLT transformation (using the document() function). I haven’t seen any of PHP’s competitor (scripting) languages come out with features as advanced as this.
Enjoy!
Please! If you’re using PHP, get the php-json functions/extensions and use JSON instead.
Fairies die when XML gets overused.
JSON is not the end all/be all…it introduces a TON of security related ramifications that need to be considered, plus XML is a standard and is already deeply rooted in the realm of programming.
The other issue with your blanket “use JSON instead” statement is that it’s a new technology and not everyone is A) using it or B) even willing to use it. When you’re passing data around a network, both parties have to agree on the expected payload and format, so you can’t just expect everyone to be using bleeding edge technology.
I’ll state now that my original comment was deliberately provocative in an attempt to stimulate some discussion.
But to address your points, the standard(Yes it is a standard) is not insecure in itself. and PHPJson does not have any security issues associated with it. What I think you’re alluding to is the edge case where Javascript programmers get JS to blindly execute JSON from an untrusted source as a parsing method. Many solutions are provided elsewhere.
You’re right that acceptance is the main barrier to any new technology but this doesn’t make the old technology any better, or stop people from advertising the advantages of the new technology. In my book the disadvantages of XML is: major information redundancy, bloat, XSLT, and data format inflexibility.
Added to the above, phpjson integrates much better into the PHP language than SimpleXML.
There’s no supprise the JSON integrates better in to the PHP language, as it is effectively a cross-platform object persistence syntax. SimpleXML (or any xml binding API for that matter) suffers from the same problem: nested programming language constructs (arrays, hashes, objects etc) cannot represent ‘complex’ XML. (I’d post a example, but the forum software would swallow it!)
I’ve heard the argument many times that XML is bloat: and looking at an XML file, you can see what these people are getting at. However, XML tries to be a generic format, so it is inevitable that this will be less succinct than a format designed for a particular data set type. The advantage of using XML is that the same set of APIs can be re-used in many different situations.
Cheers
–Robin
ps. Why is XSLT a disadvantage?
cannot represent ‘complex’ XML.
Yeah. I agree with this, tag attributes can’t be mapped directly to PHP objects, but simple workarounds could be used by any library to better integrate with PHP, by taking advantage of XMLs draconian tag and attribute naming restrictions.
However, XML tries to be a generic format
I applaud the morals behind XML and what it sets out to achieve, however it ends up just being more restrictive than something like JSON. By not defining any data-types, representing something common, like an array, becomes very verbose. JSON can represent text (arguably all the XML can do), binary data (without all the <!cdata crap), arrays, hashtables and numbers in a standardised manner. In some ways, json can be as generic as XML, it can also be much more powerful byte-for-byte.
The advantage of using XML is that the same set of APIs can be re-used in many different situations.
Yes this is an advantage, but by using this philisophy, you are restricted by the lowest-common-denominator. Sure, the XML libraries for C have a similar API to those for PHP, but the JSON C++ libs take advantage of the extra features of C++ to provide a more natural interface.
Sorry if I seem a bit agressive, I really don’t like XML .
ste.
p.s. try googling xslt considered harmful. And read about ‘Formatting Objects’
“By not defining any data-types, representing something common, like an array, becomes very verbose.”
It’s interesting you should mention that…think about it for a second, why is it difficult for XML to represent something as fundamentally simple as an array? What if (using .Net or Java) it’s an array of objects? XML has no notion of something such as an object, much less an array of them. Or a hashtable, which can be a container for pretty much any kind of underlying data structure, most of which are impossible to express in XML without adding some sort of proprietary mess to it.
Expressing arrays of types known in XML (strings, numeric primitives) is easy, and almost all languages can pass around XML docs with arrays of those types (think web services), but this has to be limited to the most basic of types.
This is an area where JSON really has an edge. Remember how long XML took to become mainstream though. JSON is relatively new to the scene, and it’ll be up to the language designers to build libraries that support it.
Edited 2007-03-01 06:04
Much as I hate to have to roll out the old adage, XML and JSON are both simply tools. In the same way that you wouldn’t want to hammer a nail into a wall with a screwdriver, you probably wouldn’t want to replace, say, HTML with a JSON equivalent (just think for a minute how that might work – it wouldn’t!).
XML and JSON are both excellent tools in their own right when applied as a sensible solution to a programming problem. What concerns me, is that this current fad of “anti XML fanboy-ism” is leading otherwise excellent programmers to overlook XML for the wrong reasons. It’s like a whole bunch of carpenters suddenly banned themselves from even thinking about using a hammer.
Try and be objective about the strengths and weaknesses of XML and JSON and whatever else you come across, and that way you’ll not fall in to the trap of using one where the other would be more suitable.
–Robin
p.s. I’m currently working on a project using XSL-FO, XSL, SOAP. Going very well, easy to set up, and about 10,000 times better than a solution programmed from scratch in PHP by a programmer who _used_ to work for my company!