note 71941 modified in ref.xml by danbrown

From: Date: Wed, 03 Mar 2010 18:54:15 +0000
Subject: note 71941 modified in ref.xml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166616@lists.php.net to get a copy of this message
Hi ! After parsing the XML and modifying it, I just add a method to rebuild the XML form the internal structure (xmlp->document). The method xmlp->toXML writes into xmlp->XML attributes. Then, you just have to output it. I hope it helps. <?php class XMLParser { var $parser; var $filePath; var $document; var $currTag; var $tagStack; var $XML; var $_tag_to_close = false; var $TAG_ATTRIBUT = 'attr'; var $TAG_DATA = 'data'; function XMLParser($path) { $this->parser = xml_parser_create(); $this->filePath = $path; $this->document = array(); $this->currTag =& $this->document; $this->tagStack = array(); $this->XML = ""; } function parse() { xml_set_object($this->parser, $this); xml_set_character_data_handler($this->parser, 'dataHandler'); xml_set_element_handler($this->parser, 'startHandler', 'endHandler'); if(!($fp = fopen($this->filePath, "r"))) { die("Cannot open XML data file: $this->filePath"); return false; } while($data = fread($fp, 4096)) { if(!xml_parse($this->parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } } fclose($fp); xml_parser_free($this->parser); return true; } function startHandler($parser, $name, $attribs) { if(!isset($this->currTag[$name])) $this->currTag[$name] = array(); $newTag = array(); if(!empty($attribs)) $newTag[$this->TAG_ATTRIBUT] = $attribs; array_push($this->currTag[$name], $newTag); $t =& $this->currTag[$name]; $this->currTag =& $t[count($t)-1]; array_push($this->tagStack, $name); } function dataHandler($parser, $data) { $data = trim($data); if(!empty($data)) { if(isset($this->currTag[$this->TAG_DATA])) $this->currTag[$this->TAG_DATA] .= $data; else $this->currTag[$this->TAG_DATA] = $data; } } function endHandler($parser, $name) { $this->currTag =& $this->document; array_pop($this->tagStack); for($i = 0; $i < count($this->tagStack); $i++) { $t =& $this->currTag[$this->tagStack[$i]]; $this->currTag =& $t[count($t)-1]; } } function clearOutput () { $this->XML = ""; } function openTag ($tag) { $this->XML.="<".strtolower ($tag); $this->_tag_to_close = true; } function closeTag () { if ($this->_tag_to_close) { $this->XML.=">"; $this->_tag_to_close = false; } } function closingTag ($tag) { $this->XML.="</".strtolower ($tag).">"; } function output_attributes ($contenu_fils) { foreach ($contenu_fils[$this->TAG_ATTRIBUT] as $nomAttribut => $valeur) { $this->XML.= " ".strtolower($nomAttribut)."=\"".$valeur."\""; } } function addData ($texte) { // to be completed $ca = array ("é", "è", "ê", "à"); $par = array ("&eacute;", "&egrave;", "&ecirc;", "agrave;"); return htmlspecialchars(str_replace ($ca, $par, $texte), ENT_NOQUOTES); } function toXML ($tags="") { if ($tags=="") { $tags = $this->document; $this->clearOutput (); } foreach ($tags as $tag => $contenu) { $this->process ($tag, $contenu); } } function process ($tag, $contenu) { // Pour tous les TAGs foreach ($contenu as $indice => $contenu_fils) { $this->openTag ($tag); // Pour tous les fils (non attribut et non data) foreach ($contenu_fils as $tagFils => $fils) { switch ($tagFils) { case $this->TAG_ATTRIBUT: $this->output_attributes ($contenu_fils); $this->closeTag (); break; case $this->TAG_DATA: $this->closeTag (); $this->XML.= $this->addData ($contenu_fils [$this->TAG_DATA]); break; default: $this->closeTag (); $this->process ($tagFils, $fils); break; } } $this->closingTag ($tag); } } } ?> --was-- Hi ! After parsing the XML and modifying it, I just add a method to rebuild the XML form the internal structure (xmlp->document). The method xmlp->toXML writes into xmlp->XML attributes. Then, you just have to output it. I hope it helps. class XMLParser { var $parser; var $filePath; var $document; var $currTag; var $tagStack; var $XML; var $_tag_to_close = false; var $TAG_ATTRIBUT = 'attr'; var $TAG_DATA = 'data'; function XMLParser($path) { $this->parser = xml_parser_create(); $this->filePath = $path; $this->document = array(); $this->currTag =& $this->document; $this->tagStack = array(); $this->XML = ""; } function parse() { xml_set_object($this->parser, $this); xml_set_character_data_handler($this->parser, 'dataHandler'); xml_set_element_handler($this->parser, 'startHandler', 'endHandler'); if(!($fp = fopen($this->filePath, "r"))) { die("Cannot open XML data file: $this->filePath"); return false; } while($data = fread($fp, 4096)) { if(!xml_parse($this->parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } } fclose($fp); xml_parser_free($this->parser); return true; } function startHandler($parser, $name, $attribs) { if(!isset($this->currTag[$name])) $this->currTag[$name] = array(); $newTag = array(); if(!empty($attribs)) $newTag[$this->TAG_ATTRIBUT] = $attribs; array_push($this->currTag[$name], $newTag); $t =& $this->currTag[$name]; $this->currTag =& $t[count($t)-1]; array_push($this->tagStack, $name); } function dataHandler($parser, $data) { $data = trim($data); if(!empty($data)) { if(isset($this->currTag[$this->TAG_DATA])) $this->currTag[$this->TAG_DATA] .= $data; else $this->currTag[$this->TAG_DATA] = $data; } } function endHandler($parser, $name) { $this->currTag =& $this->document; array_pop($this->tagStack); for($i = 0; $i < count($this->tagStack); $i++) { $t =& $this->currTag[$this->tagStack[$i]]; $this->currTag =& $t[count($t)-1]; } } function clearOutput () { $this->XML = ""; } function openTag ($tag) { $this->XML.="<".strtolower ($tag); $this->_tag_to_close = true; } function closeTag () { if ($this->_tag_to_close) { $this->XML.=">"; $this->_tag_to_close = false; } } function closingTag ($tag) { $this->XML.="</".strtolower ($tag).">"; } function output_attributes ($contenu_fils) { foreach ($contenu_fils[$this->TAG_ATTRIBUT] as $nomAttribut => $valeur) { $this->XML.= " ".strtolower($nomAttribut)."=\"".$valeur."\""; } } function addData ($texte) { // to be completed $ca = array ("é", "è", "ê", "à"); $par = array ("&eacute;", "&egrave;", "&ecirc;", "agrave;"); return htmlspecialchars(str_replace ($ca, $par, $texte), ENT_NOQUOTES); } function toXML ($tags="") { if ($tags=="") { $tags = $this->document; $this->clearOutput (); } foreach ($tags as $tag => $contenu) { $this->process ($tag, $contenu); } } function process ($tag, $contenu) { // Pour tous les TAGs foreach ($contenu as $indice => $contenu_fils) { $this->openTag ($tag); // Pour tous les fils (non attribut et non data) foreach ($contenu_fils as $tagFils => $fils) { switch ($tagFils) { case $this->TAG_ATTRIBUT: $this->output_attributes ($contenu_fils); $this->closeTag (); break; case $this->TAG_DATA: $this->closeTag (); $this->XML.= $this->addData ($contenu_fils [$this->TAG_DATA]); break; default: $this->closeTag (); $this->process ($tagFils, $fils); break; } } $this->closingTag ($tag); } } } http://php.net/manual/en/ref.xml.php

« previous php.notes (#166616) next »