PHP處理XML的例項

jieforest發表於2012-09-13
XML is a format created in the late 1990s with the goal to be used in applications that need to interchange information in a format that is human-readable and is independent operating systems on which the applications run. XML was like HTML, which everybody was aware, but imposes more strict structure rules.

Many data formats were created around XML. However, XML is still a pain to write manually. Personally, one of the things I find more painful is to have to write every tag name twice: one for opening a tag and another for closing the tag.

A few years ago there was an attempt to define a new specification for the eventual XML 2.0. That attempt seems to have died. I recall that I proposed to allow tags to have a short close notation to avoid repeating the tag name. It would be like this:

data

The proposal was rejected. It seems that the organizers had no interest in avoiding one of the XML pain points. Given that, I am not surprised if XML 2.0 is really dead.

Nowadays, developers tend to use simpler formats like YAML and JSON to exchange data in a human-readable format. Ten years ago they would have used XML instead.

Despite the change in the mentality of the developers, once in a while we still need to deal with XML for some reason.

For instance, a few years ago I needed to develop a client and server of OpenID protocol. That is a single sign-on protocol, i.e. it allows you to login in multiple sites using the same account. It was used to let users login in the JSClasses site with the same account you use in the PHPClasses site.

OpenID itself is yet another story of a protocol design that is a pain to implement, but I will leave that story for a later article after I publish the OpenID classes that I developed.

What matters is that OpenID uses the XRDS protocol, which is meant to allow clients to discover the addresses and the features supported by the servers they need to access. XRDS is based in XML too.

Another situation for which XML may be better suited than other more simple formats, is when you need to represent certain entities that do not map to the data types used in your programming language, otherwise JSON would probably be a better choice.

For instance when you need to represent templates with special placeholders, you can use special XML tags to represent the placeholders. This is not a common situation but sometimes you may need to deal with it and XML may still be a good option.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-743332/,如需轉載,請註明出處,否則將追究法律責任。

相關文章