Você pode usar o leitor de XML que criei se quiser suporte a PHP4.
Não faz análise nem nada... tem outra finalidade, mas já faz o que vc quer.
Eis o código:
Arquivo
class.AOP_XMLReader.php:
<?php
require_once "class.AOP_XMLElement.php";
class AOP_XMLReader
{
var $_parser;
var $_curEl;
var $documentElement;
function AOP_XMLReader()
{
$this->_curEl = null;
$this->_parser = xml_parser_create();
xml_set_object($this->_parser, $this);
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($this->_parser, "_startElement", "_endElement");
xml_set_character_data_handler($this->_parser, "_CDATAElement");
}
function & fromString($xmlContent)
{
$xmlReader = new AOP_XMLReader($xmlContent);
return $xmlReader->_parse($xmlContent);
}
function & _parse($data)
{
if (!xml_parse($this->_parser, $data)) {
$errMessage = xml_error_string(xml_get_error_code($this->_parser));
$errLine = xml_get_current_line_number($this->_parser);
xml_parser_free($this->_parser);
die(sprintf("XML error: %s at line %d", $errMessage, $errLine));
}
xml_parser_free($this->_parser);
return $this->documentElement;
}
function _startElement($parser, $tagName, $attrs)
{
$el = & new AOP_XMLElement($tagName);
$el->setAttributes($attrs);
$el->setParentNode($this->_curEl);
if ($this->_curEl !== null) {
$this->_curEl->addChildNode($el);
} else {
$this->documentElement = & $el;
}
$this->_curEl = & $el;
}
function _endElement($parser, $tagName)
{
if ($this->_curEl !== null) {
if ($this->_curEl->getTag() != $tagName) {
trigger_error(
"<b>[Error]:</b> XML Node '" . $tagName . "' is not in the right inheritance!",
E_USER_ERROR
);
}
$this->_curEl = & $this->_curEl->getParentNode();
}
}
function _CDATAElement($parser, $data)
{
if (strlen(trim($data)) > 0) {
$cdata = & new AOP_XMLElement("#text");
$cdata->setValue($data);
$cdata->setParentNode($this->_curEl);
$this->_curEl->addChildNode($cdata);
}
}
}
?>
Arquivo
class.AOP_XMLElement.php:
<?php
class AOP_XMLElement
{
var $tag;
var $value;
var $attributes;
var $childNodes;
var $parentNode;
function AOP_XMLElement($tag = "")
{
if ($tag != "") {
$this->setTag($tag);
}
$this->attributes = array();
$this->childNodes = array();
}
function setTag($tag) { $this->tag = $tag; }
function getTag() { return $this->tag; }
function setValue($value) { $this->value = $value; }
function getValue() { return $this->value; }
function setParentNode(& $parent) { $this->parentNode = & $parent; }
function & getParentNode() { return $this->parentNode; }
function setAttributes($attrs) { $this->attributes = $attrs; }
function getAttributes() { return $this->attributes; }
function setAttribute($attrName, $attrValue)
{
$this->attributes[$attrName] = $attrValue;
}
function getAttribute($attrName)
{
if ($this->hasAttribute($attrName)) {
return $this->attributes[$attrName];
}
return null;
}
function hasAttribute($attrName)
{
if (array_key_exists($attrName, $this->attributes)) {
return true;
}
return false;
}
function setChildNodes($nodes) { $this->childNodes = $nodes; }
function & getChildNodes() { return $this->childNodes; }
function addChildNode(& $node)
{
$this->childNodes[count($this->childNodes)] = & $node;
}
function & getChildNode($i)
{
if (array_key_exists($i, $this->childNodes)) {
return $this->childNodes[$i];
}
return null;
}
}
?>
Forma de uso:
<?php
require_once "class.AOP_XMLReader.php";
// Get file contents
$fContent = implode("", file("arquivo.xml"));
// Parse XML file into an array map
$XmlReader = AOP_XMLReader::fromString($fContent);
// Displaying contents
die("<pre>".print_r($XmlReader, true)."</pre>");
?>
Agora vc adapta este código pro seu uso pessoal. Se quiser adicionar uns créditos... agradeço.
[]s,