note 76748 added to function.xml-parse

From: Date: Sun, 29 Jul 2007 02:42:48 +0000
Subject: note 76748 added to function.xml-parse
Groups: php.notes 
Request: Send a blank email to php-notes+get-129016@lists.php.net to get a copy of this message
This page has been a great help! I've adapted the examples below to make a class to parse an X(HT)ML file to a multidimensional array. Spaces, tabs, breaks, etc. are included in the array as TEXT_NODE, since in XHTML they may be functional. A function to trim them can easily be added if so desired. I've written my class to store multiple tags with the same name. In order to do this I've used nested arrays with the key NODES to store the order in which tags and data were parsed. These NODES arrays can be used as a blueprint to reconstruct the X(HT)ML part of the document in it's entirity, including formatting. (Doctype will have to be added for validity). <?php class my_xml_object { var $xml_data; # raw xml data from file function parse_xml_file($my_uri) { $this->xml_data = null; # clear previously parsed file and related variables if (is_file($my_uri) && is_readable($my_uri)) { # existing and readable uri? $my_file = fopen($my_uri, "r"); while($my_xml_input = fread($my_file, filesize($my_uri))) { $this->xml_data .= $my_xml_input; # add data to xml_data } fclose($my_file); $this->parse_xml_data($this->xml_data); # parse data } else { trigger_error("supplied argument is not a URI to a (readable) file", E_USER_ERROR); } } var $xml_array = Array(); # xml array from parsed data function parse_xml_data($my_data) { # adapted from class by randlem@gmail.com, tgrabietz@bupnet.de, bbellwfu@gmail.com, Kyle Bresin - see http://nl2.php.net/xml_parse $this->xml_array = Array(0 => Array()); # clear previously parsed file and related variables; populate first element $my_parser = xml_parser_create(); # set up parser xml_set_object($my_parser, $this); # enable parser within object xml_set_element_handler($my_parser, "xml_tag_open", "xml_tag_close"); xml_set_character_data_handler($my_parser, "xml_tag_data"); if (!xml_parse($my_parser, $my_data)) { trigger_error("data can not be parsed", E_USER_ERROR); # inspect problems #die(sprintf("<br />\n<b>Error</b>: %s on line <b>%d</b><br />\n", xml_error_string(xml_get_error_code($my_parser)), xml_get_current_line_number($my_parser))); } xml_parser_free($my_parser); # free parser return $this->xml_array; # return xml array } var $my_branch = Array(); function xml_tag_open($my_parser, $my_name, $my_attributes) { array_push($this->my_branch, $my_name); # add tag name to branch $this->xml_array[] = Array(); # nest array in xml array for data if (count($my_attributes)) { $this->xml_array[count($this->xml_array) - 1]["ATTRIBUTES"] = $my_attributes; # nest attributes array } } function xml_tag_data($my_parser, $my_data) { $this->xml_array[count($this->my_branch)]["TEXT_NODE"][] = $my_data; # add data to nested array $this->xml_array[count($this->my_branch)]["NODES"][] = "TEXT_NODE"; # add text node to nested NODES array } function xml_tag_close($my_parser, $my_name) { $this->xml_array[count($this->my_branch) - 1][$this->my_branch[count($this->my_branch) - 1]][] = $this->xml_array[count($this->xml_array) - 1]; # nest arrays to follow document structure array_pop($this->xml_array); # pop off element that was nested $this->xml_array[count($this->my_branch) - 1]["NODES"][] = $my_name; # add tag node to nested NODES array array_pop($this->my_branch); # update branch } ?> ---- Server IP: 194.109.193.76 Probable Submitter: 80.56.206.193 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse.php Edit -- https://master.php.net/note/edit/76748 Del: integrated -- https://master.php.net/note/delete/76748/integrated Del: useless -- https://master.php.net/note/delete/76748/useless Del: bad code -- https://master.php.net/note/delete/76748/bad+code Del: spam -- https://master.php.net/note/delete/76748/spam Del: non-english -- https://master.php.net/note/delete/76748/non-english Del: in docs -- https://master.php.net/note/delete/76748/in+docs Del: other reasons-- https://master.php.net/note/delete/76748 Reject -- https://master.php.net/note/reject/76748 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129016) next »