Hey everyone, I have an issue and I know that I need to modify the code I am using but am not sure how to..
I am parsing a large xml string breaking it into named session arrays so I can use the data on a seperate page without having to store it in a temp db...The issue that I am running into is that for the most part I have a parent tag and one child tag per block. I have one block that has a Parent, Child and a Sibling
(my terms might be wrong there)
exp of Parent Child
<Corporate>
<CorpBus>
</CorpBus>
<Corporate
In my Parent, Child, Sibling setup
<Kiosks>
<Kiosk>
<KioskInfo>
</KioskInfo>
<Attachments>
<Attachment>
</Attachment>
</Attachments>
</Kiosk>
<Kiosks>
It is at the step of <Attachments><Attachment> where I am getting my issue.
In the parse I get something like this for the first
[‘xml’][‘Corporate’][‘0’][‘CorpBus’][‘0’]
Looping I get
[‘xml’][‘Corporate’][‘0’][‘CorpBus’][‘1’]
[‘xml’][‘Corporate’][‘0’][‘CorpBus’][‘2’]
[‘xml’][‘Corporate’][‘0’][‘CorpBus’][‘3’]
etc….and this is perfect as to how I need it….
For the other I get…
[‘xml’][‘Kiosks’][‘0’][‘Kiosk’][‘0’][‘KioskInfo’][‘0’]
And
[‘xml’][‘Kiosks’][‘0’][‘Kiosk’][‘0’][‘Attachments’][‘0’][‘Attachment’][‘0’]
The only spot that gets incremented is at
[‘xml’][‘Kiosks’][‘0’][‘Kiosk’][‘1’] etc…
I need to increment at [‘KioskInfo’][‘0’] and again at [‘Attachment’][‘0’]
Here is the parsing that I am using that gets me one level deep….and I just can no figure out the modification that I need to make….
<?PHP
/**
* XMLToArray Generator Class
* Purpose : Creating Hierarchical Array from XML Data
*/
class XmlToArray
{
var $xmlResponse='';
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function XmlToArray($xmlResponse)
{
$this->xmlResponse = $xmlResponse;
}
/**
* _struct_to_array($values, &$i)
*
* This adds the contents of the return xml into the array for easier processing.
* Recursive, Static
*
* @access private
* @param array $values this is the xml data in an array
* @param int $i this is the current location in the array
* @return Array
*/
function _struct_to_array($values, &$i)
{
$child = array();
if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
while ($i++ < count($values)) {
switch ($values[$i]['type']) {
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
if(!empty($name)){
$child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
if(isset($values[$i]['attributes'])) {
$child[$name] = $values[$i]['attributes'];
}
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;
case 'close':
return $child;
break;
}
}
return $child;
}//_struct_to_array
/**
* createArray($data)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access public
* @param string $data this is the string of the xml data
* @return Array
*/
function createArray()
{
$xmlResponse = $this->xmlResponse;
$values = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xmlResponse, $values);
xml_parser_free($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
$array[$name] = $this->_struct_to_array($values, $i);
return $array;
}//createArray
}//XmlToArray
?>
Any direction would be helpful…
Thanks