Simple XML DOM parser for PHP
Sterling Camden
I realize that I’ve just added to the pile of reinvented wheels here, but I needed something simpler than SimpleXML (particularly in the requirements department) that nevertheless translated the event-based model of PHP’s built-in parser to a DOM model.
Thus, I created the very simple parser which you can download below. It’s only 67 lines long. It’s partially based on the example from wolfon, but simplified even further by adding an xmlElement class instead of trying to force all of the relationships into an array format.
To use:
require("xmlparse.php");
$dom = new xmlDOM($xml_text); // Parses $xml_text
$dom->root is an xmlElement with a zero-length tagName whose children member is an array of the top-level elements (of which there should only be 1) .
$dom->find_tag($tagName, $from);
Returns the first xmlElement whose tagName matches $tagName, starting with the children of $from. If $from is null, it starts from the root. If not found, false is returned.
An xmlElement has the following members:
parent – the parent element
tagName – the tag name
attributes – an associative array of name => value
children – an array of child elements
data – any character data within the element
find_tag($tagName) – finds a descendant element with the specified tag name. Returns false if none found.
Posted in PHP, XML |
2 Comments » RSS 2.0 | Sphere it!





In general, “simplicity == beauty” is a helpful rule of thumb, but I’m not sure that’s really entirely accurate in the case of PHP. Still . . . nice work.
Yes, in PHP you really need to know if simplicity === beauty.
Thanks.