note 81910 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:56:29 +0000 |
| Subject: | note 81910 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166628@lists.php.net to get a copy of this message | ||
Note Submitter: shawn dot rapp at gmail dot com
----
The reason why you would want to make your own simplistic DOM parser is because a lack of compatible
between PHP 4's domxml and PHP 5's dom.
So it is for portability without having to wrapper the two different DOMs.
If you need a simple light weight XML parser that is portable this is the best way. If you are
writing applications for a particular server and more concerned with functionality and speed go with
a compiled in DOM.
Here is the fix to Emmetts code...
<?PHP
$fp = fopen("test.xml","r");
$xml = fread($fp, 4096);
fclose($fp);
$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 (!is_array($this->pointer['child_nodes'])){
$this->pointer['child_nodes'] = array();
}
return count($this->pointer['child_nodes']);
}
function tag_open($parser, $tag, $attributes) {
$idx = $this->makeChildNode();
$this->pointer['child_nodes'][$idx] = Array(
'_idx' => $idx,
'_parent' => &$this->pointer,
'tag_name' => $tag,
'attributes' => $attributes,
);
$this->pointer =& $this->pointer['child_nodes'][$idx];
}
function cdata($parser, $cdata) {
//drop text nodes that are just white space formatting characters
if (trim($cdata) != "") {
$idx = $this->makeChildNode();
$this->pointer['child_nodes'][$idx] = $cdata;
}
}
function tag_close($parser, $tag) {
$idx =& $this->pointer['_idx'];
$this->pointer =& $this->pointer['_parent'];
unset($this->pointer['child_nodes'][$idx]['_idx']);
unset($this->pointer['child_nodes'][$idx]['_parent']);
}
}
?>