KBD

Keith Devens .com

Tuesday, August 19, 2008 Flag waving
Cause she's a better fighter than you are, that's why. She's younger, she's stronger, and she'... – "Frankie Dunn" (Million Dollar Baby)

PHP XML Library, version 1.2b

Introduction

Face 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
include('xml.php');
$data XML_unserialize($xml);
?>
<?php
include('xml.php');
$xml XML_serialize($data);
?>

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.

Notes

PHP 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:
php_flag allow_call_time_pass_reference on

Download the source

Here's the source code (just one file), and if you want pretty colors, here's the syntax highlighted version.

Contact me

If you find any bugs or have any feature requests, or just want to say "hi" or "thanks", please drop me a line.

History

May 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)

Index

A B C D E F G H I J L M N O P R S T U V W X

All pages

A

  1. About
  2. Acno's Energizer
  3. Artificial Intelligence
  4. ASP.NET
  5. Atom

B

  1. Bash
  2. Belief systems
  3. Bookmarklets
  4. Build tools

C

  1. C and C++
  2. C#
  3. C++ Reference
  4. Calvinism
  5. Cars I want to consider
  6. CGI
  7. character sets
  8. Chess
  9. Christian Reconstruction
  10. Christian Resources
  11. Chronicles of Narnia
  12. Color tools
  13. Computer Science
  14. Cornelius Van Til
  15. CSS - Cascading style sheets
  16. CSSTabs

D

  1. Database
  2. Differencing programs
  3. Documentation standards
  4. Downloads
  5. Dualities
  6. Dvorak keyboard

E

  1. E-mail me
  2. Eclipse
  3. Eiffel
  4. Emacs
  5. Evolution
  6. Extension languages

F

  1. File extensions
  2. Firefox
  3. Formation: web form automation library for PHP
  4. Forth

G

  1. Greg Bahnsen
  2. GUI Toolkits
  3. Guns

H

  1. Hex editors

I

  1. Important articles or essays
  2. Installers
  3. Internet radio stations

J

  1. Java
  2. Javascript
  3. jEdit

L

  1. Linux
  2. Lisp
  3. Logical fallacies
  4. Lua

M

  1. Markup
  2. Miscellaneous Links
  3. mod_rewrite
  4. Movie theaters
  5. My comment policy
  6. My essential programs
  7. My resume

N

  1. Namespaces
  2. Naming conventions
  3. New Years 2000
  4. N^2 sort comparison

O

  1. Open Source License
  2. OPML

P

  1. Perl
  2. Philosophy
  3. PHP
  4. PHP Calendar (version 2.3)
  5. PHP XML Library, version 1.2b
  6. Pictures
  7. Postmillenialism
  8. Presuppositionalism
  9. Programming Fonts
  10. Programming languages
  11. Programming Resources
  12. Punta Cana
  13. Python

R

  1. RDF
  2. REBOL
  3. Reflex game
  4. Regular expressions
  5. Religion
  6. RFCs
  7. Robot Exclusions
  8. Roman Catholicism
  9. Ruby

S

  1. Scala programming language
  2. Science
  3. Shorthand
  4. Skydiving, August 28, 2000
  5. Software I've written
  6. SPAM
  7. SQLite
  8. StructuredText

T

  1. Tabs vs Spaces
  2. Tcl/Tk
  3. Tea
  4. Text Editors
  5. TextDrive
  6. The Big Bang
  7. The naked street
  8. Theonomy
  9. Tools of communication

U

  1. Unicode
  2. URL Design

V

  1. Version control systems
  2. VI text editor
  3. Virtual machines

W

  1. WeblogUrls
  2. Wiki
  3. WikiBlogIntegration
  4. World of Warcraft
  5. wxWidgets

X

  1. XHTML
  2. XML
  3. XML to PHP translator
  4. XML-RPC
  5. XML-RPC Library for PHP (v 2.5)

Generated in about 0.035s.

(Used 4 db queries)

mobile phone