Keith Devens .com |
Thursday, March 18, 2010 | ![]() |
| C++ is history repeated as tragedy. Java is history repeated as farce. – Scott McKay | ||
PHP XML Library, version 1.2bIntroductionFace it, XML is a pain. This simple library tries to make it much less painful. It allows you to easily parse XML into a PHP data structure, and it allows you to serialize PHP data structures into XML. Many other programming languages have easy-to-use XML interfaces (Python's is particularly nice, AFAIK. Lots of things in Python are like that), but there's nothing easy to use built into PHP, and you typically have to write your own SAX-style parser even to deal with simple XML. However, for PHP 5, SimpleXML looks very nice. It still doesn't get you PHP data structures, but it does lots of other nice things. Personally, I think this library provides the easiest way to use data-oriented XML in PHP, regardless of what verison you're using. Note that this library only deals with data-oriented XML, as opposed to document-oriented XML. Data-oriented XML is fully hierarchical, such as: <foo><bar>blah</bar><baz>roar</baz></foo>, and the order of bar and baz isn't significant. Document-oriented XML can have "mixed content". The most common example of document-oriented XML is HTML, where you can have things like <p>Here's a <a href="URL">link</a> to my <em>favorite</em> web page.</p>. Because most XML is data-oriented, and can fit in memory comfortably, the approach this library takes is a good fit. If the XML is document-oriented, or too large to fit in memory easily, this library isn't appropriate for you. Examples
<?php
<?php
And literally, that's it. I've set up an XML to PHP translator that lets you submit an XML file so you can see what the resulting PHP data structure looks like after parsing a given XML file. NotesPHP 4 deprecated a feature called call-time pass-by-reference. Unfortunately, the developers of PHP deprecated the feature without making appropriate alternatives available, so the net result is that, under PHP 4, some things are literally impossible to do without using call-time pass-by-reference even though it's deprecated. Even parts of the PHP manual use the deprecated feature since it's impossible not to. This library requires that call-time pass-by-reference be enabled. Fortunately, it's enabled by default in PHP 4. I'm not sure what the default is in PHP 5, but in PHP 5 it's no longer necessary. Once PHP 5 comes out I'll make a new version of this library specifically for PHP 5 that doesn't use the deprecated feature. To enable this feature you can add the following line to a .htaccess file: Download the sourceHere's the source code (just one file), and if you want pretty colors, here's the syntax highlighted version. Contact meIf you find any bugs or have any feature requests, or just want to say "hi" or "thanks", please drop me a line. HistoryMay 9, 2004: Version 1.0 released May 28, 2004: Version 1.1 released. XML_serialize function updated to concatenate strings instead of build arrays (it's faster that way), and to fix a bug which manifests itself upon repeated calls to XML_serialize with the same data (the data wasn't reset() as was needed). June 4, 2004: Version 1.2 released. XML_serialize function updated to use output buffering instead of concatenating strings (it's faster that way). Other than that, some code reformatting was done (now weighs in at <100 lines not counting block comments) and XML_unserialize was updated to concatenate strings instead of building arrays when capturing cdata. July 27, 2004: Version 1.2b. Fixed a small bug. It turns out PHP handles "0" (the string) as false, and if you had "0" as the value of an element, NULL would be returned instead of "0". In addition, empty tags are now returned as the empty string, not NULL. Page last edited: November 17, 2005 (utc) |
IndexA B C D E F G H I J L M N O P R S T U V W X All pagesABC
DEFGHIJLMNOP
RS
T
UVWXGenerated in about 0.03s. (Used 4 db queries) |