note 81910 added to ref.xml
| From: | shawndotrappatgmaildotcom at osu1 dot php dot net | Date: | Wed, 19 Mar 2008 02:52:06 +0000 |
| Subject: | note 81910 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137191@lists.php.net to get a copy of this message | ||
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']);
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 66.82.9.92
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/81910
Del: integrated -- https://master.php.net/note/delete/81910/integrated
Del: useless -- https://master.php.net/note/delete/81910/useless
Del: bad code -- https://master.php.net/note/delete/81910/bad+code
Del: spam -- https://master.php.net/note/delete/81910/spam
Del: non-english -- https://master.php.net/note/delete/81910/non-english
Del: in docs -- https://master.php.net/note/delete/81910/in+docs
Del: other reasons-- https://master.php.net/note/delete/81910
Reject -- https://master.php.net/note/reject/81910
Search -- https://master.php.net/manage/user-notes.php