Chapter 11. Parsing and Generating XML

With XML, you can effortlessly exchange data between programs written in different languages, running on different operating systems, located on computers anywhere in the world. At least, that’s what enthusiastic computer programmers and salespeople who work for companies that sell XML tools will tell you. They’re sort of telling the truth. XML does make it easier to trade structured information between two programs. But you still have to do some work to herd your data into the right structure. This chapter shows you how to do that work with PHP.

XML is a markup language that looks a lot like HTML. An XML document is plain text and contains tags delimited by < and >. There are two big differences between XML and HTML:

  • XML doesn’t define a specific set of tags you must use.

  • XML is extremely picky about document structure.

In one sense, XML gives you a lot more freedom than HTML. HTML has a certain set of tags: the <a></a> tags surround a link, the <ul></ul> tags denote an unordered list, the <li></li> tags indicate a list element, and so on. An XML document, however, can use any tags you want. Put <rating></rating> tags around a movie rating, <height></height> tags around someone’s height, or <favoritecolor></favoritecolor> tags around someone’s favorite color—XML doesn’t care. Of course, whomever (or whatever program) you’re sharing the XML document with also needs to agree to use and understand the same set of tags.

While you get more freedom ...

Get Learning PHP 5 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.