note 75953 added to function.xml-parse
| From: | php at b635 dot com | Date: | Sat, 23 Jun 2007 17:17:10 +0000 |
| Subject: | note 75953 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127902@lists.php.net to get a copy of this message | ||
The suggestions below have been a great help, but there was one thing I really needed...
I'm parsing Amazon XML data, and I wanted to be able to index into the array using something
like:
<?php
print "<p>" .
$strAXML->arrOutput[ITEMLOOKUPRESPONSE][ITEMS][ITEM][SMALLIMAGE][URL];
?>
To solve this, I had to push all of the open tags onto a separate stack, add any tag data to the
tail end of an attributed multi-dimensional array, and then pop the tag name off of the stack once
it was closed...
<?php
class xml2array {
var $arrOutput = array();
var $arrName = array();
var $objParser;
var $strXmlData;
function parse($strInputXML) {
// standard XML parse object setup
$this->objParser = xml_parser_create ();
xml_set_object($this->objParser,$this);
xml_set_element_handler($this->objParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->objParser, "tagData");
$this->strXmlData = xml_parse($this->objParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->objParser)),
xml_get_current_line_number($this->objParser)));
}
xml_parser_free($this->objParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
// push the current tag name to an array of still-open tag names
array_push ($this->arrName, $name);
// merge the array of current attributes to the open tag
// NOTE: this does not currently handle multiple attributes with the same name
// (i.e. it will overwrite them with the last values)
$strEval = "\$this->arrOutput";
foreach ($this->arrName as $value) {
$strEval .= "[" . $value . "]";
}
$strEval = $strEval . " = array_merge (" . $strEval . ",\$attrs);";
eval ($strEval);
}
function tagData($parser, $tagData) {
// set the latest open tag equal to the tag data
$strEval = "\$this->arrOutput";
foreach ($this->arrName as $value) {
$strEval .= "[" . $value . "]";
}
$strEval = $strEval . " = \$tagData;";
eval ($strEval);
}
function tagClosed($parser, $name) {
// pop this tag (and any subsequent tags) off the stack of open tag names
for ($i = count ($this->arrName) - 1; $i > 0; $i--) {
$currName = $this->arrName[$i];
array_pop ($this->arrName);
if ($currName == $name) {
break;
}
}
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 71.96.135.231
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- https://master.php.net/note/edit/75953
Del: integrated -- https://master.php.net/note/delete/75953/integrated
Del: useless -- https://master.php.net/note/delete/75953/useless
Del: bad code -- https://master.php.net/note/delete/75953/bad+code
Del: spam -- https://master.php.net/note/delete/75953/spam
Del: non-english -- https://master.php.net/note/delete/75953/non-english
Del: in docs -- https://master.php.net/note/delete/75953/in+docs
Del: other reasons-- https://master.php.net/note/delete/75953
Reject -- https://master.php.net/note/reject/75953
Search -- https://master.php.net/manage/user-notes.php