note 79744 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:56:11 +0000 |
| Subject: | note 79744 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166626@lists.php.net to get a copy of this message | ||
Note Submitter: emmett dot thesane at yahoo dot com
----
There's a couple of vital flaws in aquariusrick's example:
1. Multiple tags of the same name will overwrite one another.
2. Text nodes within an element are all strung together, with no information saved regarding their
order with respect to non-text nodes.
It provided a good starting point, however, for a DOM-builder that *does* allow those things. This
should be a more familiar structure for people used to DOM-walking in the browser; children of each
node are stored in "childNodes". Text nodes are simply a child node that is only a string,
instead of an array.
$xml_parser = new xml();
$xml_parser->parse($xml);
$dom = $xml_parser->dom;
print_r($dom);
class xml {
var $parser;
var $pointer;
var $dom;
function xml() {
$this->pointer =& $this->dom;
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
}
function parse($data) {
xml_parse($this->parser, $data);
}
function makeChildNode() {
if (!isset($this->pointer['childNodes'])){
$this->pointer['childNodes'] = array();
}
return count($this->pointer['childNodes']);
}
function tag_open($parser, $tag, $attributes) {
$idx = $this->makeChildNode();
$this->pointer['childNodes'][$idx] = Array(
'_idx' => $idx,
'tagName' => $tag,
'parentNode' => &$this->pointer,
'attributes' => $attributes,
);
$this->pointer =& $this->pointer['childNodes'][$idx];
}
function cdata($parser, $cdata) {
$idx = $this->makeChildNode();
$this->pointer['childNodes'][$idx] = $cdata;
//text node -- has no other attributes than the content
}
function tag_close($parser, $tag) {
$idx =& $this->pointer['_idx'];
$this->pointer =& $this->pointer['_parent'];
unset($this->pointer['childNodes'][$idx]['_idx']);
}
}