note 84261 added to function.xml-parse-into-struct

From: Date: Sun, 06 Jul 2008 07:33:48 +0000
Subject: note 84261 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-141213@lists.php.net to get a copy of this message
Had a comment up that was removed saying I'd found drawbacks, but here's a class what I wrote which works fine for me. Comments removed due to post restrictions. Pass the xml into the parse method or in the constructor. Php4 and 5 safe. class XMLThing { var $rawXML; var $valueArray = array(); var $keyArray = array(); var $parsed = array(); var $index = 0; var $attribKey = 'attributes'; var $valueKey = 'value'; var $cdataKey = 'cdata'; var $isError = false; var $error = ''; function XMLThing($xml = NULL) { $this->rawXML = $xml; } function parse($xml = NULL) { if (!is_null($xml)) { $this->rawXML = $xml; } $this->isError = false; if (!$this->parse_init()) { return false; } $this->index = 0; $this->parsed = $this->parse_recurse(); $this->status = 'parsing complete'; return $this->parsed; } function parse_recurse() { $found = array(); $tagCount = array(); while (isset($this->valueArray[$this->index])) { $tag = $this->valueArray[$this->index]; $this->index++; if ($tag['type'] == 'close') { return $found; } if ($tag['type'] == 'cdata') { $tag['tag'] = $this->cdataKey; $tag['type'] = 'complete'; } $tagName = $tag['tag']; if (isset($tagCount[$tagName])) { if ($tagCount[$tagName] == 1) { $found[$tagName] = array($found[$tagName]); } $tagRef =& $found[$tagName][$tagCount[$tagName]]; $tagCount[$tagName]++; } else { $tagCount[$tagName] = 1; $tagRef =& $found[$tagName]; } switch ($tag['type']) { case 'open': $tagRef = $this->parse_recurse(); if (isset($tag['attributes'])) { $tagRef[$this->attribKey] = $tag['attributes']; } if (isset($tag['value'])) { if (isset($tagRef[$this->cdataKey])) { $tagRef[$this->cdataKey] = (array)$tagRef[$this->cdataKey]; array_unshift($tagRef[$this->cdataKey], $tag['value']); } else { $tagRef[$this->cdataKey] = $tag['value']; } } break; case 'complete': if (isset($tag['attributes'])) { $tagRef[$this->attribKey] = $tag['attributes']; $tagRef =& $tagRef[$this->valueKey]; } if (isset($tag['value'])) { $tagRef = $tag['value']; } break; } } return $found; } function parse_init() { $this->parser = xml_parser_create(); $parser = $this->parser; xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); if (!$res = (bool)xml_parse_into_struct($parser, $this->rawXML, $this->valueArray, $this->keyArray)) { $this->isError = true; $this->error = 'error: '.xml_error_string(xml_get_error_code($parser)).' at line '.xml_get_current_line_number($parser); } xml_parser_free($parser); return $res; } } ---- Server IP: 92.48.74.199 Probable Submitter: 87.194.13.251 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- https://master.php.net/note/edit/84261 Del: integrated -- https://master.php.net/note/delete/84261/integrated Del: useless -- https://master.php.net/note/delete/84261/useless Del: bad code -- https://master.php.net/note/delete/84261/bad+code Del: spam -- https://master.php.net/note/delete/84261/spam Del: non-english -- https://master.php.net/note/delete/84261/non-english Del: in docs -- https://master.php.net/note/delete/84261/in+docs Del: other reasons-- https://master.php.net/note/delete/84261 Reject -- https://master.php.net/note/reject/84261 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141213) next »