zerodash Posted October 23, 2006 Posted October 23, 2006 I didn't want do derail any threads, so I decided to post this here: The following problem I am having is with code derived from the google_checkout contribution, which I need to modify a bit to match my needs. See this nifty XML parser: <? class XmlParser { var $params= array(); var $level = array(); function XmlParser($input) { $xmlp = xml_parser_create(); xml_parse_into_struct($xmlp, $input, $vals, $index); xml_parser_free($xmlp); $this->updateMembers($vals, $index); } // Takes the data and puts it into PHP array function updateMembers($vals, $index) { foreach ($vals as $xml_elem) { if ($xml_elem['type'] == 'open' ) { $this->level[$xml_elem['level']] = strtolower($xml_elem['tag']); } elseif ($xml_elem['type'] == 'complete') { $xml_elem['tag'] = strtolower($xml_elem['tag']); $start_level = 1; $php_stmt = '$this->params'; while($start_level < $xml_elem['level']) { $php_stmt .= '[$this->level['.$start_level.']]'; $start_level++; } $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];'; eval($php_stmt); } } } function getRoot() { return $this->level[1]; } function getData() { return $this->params; } } $xmlp = new XmlParser($xml_response); $root = $xmlp->getRoot(); $data = $xmlp->getData(); ?> It works really well except for one problem- When parsing XML formatted something like shown below, it only stores one "item" from the "item" element in the resulting array. <order> <date>2006-09-19</date> <cart> <items> <item> <quantity>1</quantity> <unit-price currency=\"USD\">80.0</unit-price> <item-name>Garbage</item-name> <item-description>Smelly</item-description> </item> <item> <quantity>2</quantity> <unit-price currency=\"USD\">10.0</unit-price> <item-name>Another Item</item-name> <item-description>This item Sucks</item-description> </item> <item> <quantity>3</quantity> <unit-price currency=\"USD\">450.0</unit-price> <item-name>nachos</item-name> <item-description>Cheesey</item-description> </item> </items> </cart> </order> For instance, when I output echo $data[$root]['cart']['items']['item']['item-name'] . "<br>"; , it will only echo "nachos". Presumably because as it loops through the parser, it copies over the "item" element over and over. Does anyone have any idea what I can do to make sure all "items" are stored in this array? I cannot alter the XML in any way, so the parser needs to be fixed. Thanks in advance for any input.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.