I will try my best to describe what I have going on…
1) I have 15 form pages, well over 500 potential fields, which are written in PHP. While most pages are one time entry forms, there are 5 that can be “recycled” as many times as needed. An example would be the Contacts Form. A user can give me 1 contact and move on OR they can “Save” the contact and add another. They may add as many contacts as they wish (up to 50).
2) I am saving the input to the forms in a temp mySql db and at the point of submission, generating a massive XML string and sending to a middle ware app written by someone else in-house. (all easy enough so far)
3) Previously the middle ware sent me a response XML string that was very simple.
Expand|Select|Wrap|Line Numbers
- <Response>
- <insert_status>Accepted</insert_status>
- </Response>.
Now the requirements have changed and the response string will consist of EVERY value sent to the middleware and its conversion state i.e. a shortened name to meet the field or format requirements of the db (Example: John Smith would return as JOHN-SM001 etc.). Now I will need to parse, reorganize, pass the information into a PHP $_SESSION array and present the information back to the user on the Insert Complete page.
The issue is this, using contacts as the example, if I am returned the following string part..
Expand|Select|Wrap|Line Numbers
- <Contacts>
- <Contact>
- <contact_id>833</contact_id>
- <contact_type>ACCOUNTING</contact_type>
- <first_name>NATALIE</first_name>
- <last_name>GEWN</last_name>
- <middle_i>M</middle_i>
- <address1>147 OCEAN DR</address1>
- <address2></address2>
- <state_cd>MA</state_cd>
- <city>RIVERMORE</city>
- <zip>01556</zip>
- <cntry_cd>840</cntry_cd>
- <telephone>9879879877</telephone>
- <mobile></mobile>
- <email_id>NGEWN@MYCO.COM</email_id>
- <department></department>
- <title></title>
- <fax>3213213211</fax>
- <url></url>
- <country>UNITED STATES OF AMERICA</country>
- <state>MASSACHUSETTS</state>
- <customer_care_phone>9879879877</customer_care_phone>
- </Contact>
- </Contacts>
Right now I can return the string and print the array in a multidimensiona l array using this class.
[PHP]
<?PHP
class XmlToArray
{
var $xml='';
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function XmlToArray($xml )
{
$this->xml = $xml;
}
/**
* _struct_to_arra y($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_arra y($values, &$i)
{
$child = array();
if (isset($values[$i]['value'])) array_push($chi ld, $values[$i]['value']);
while ($i++ < count($values)) {
switch ($values[$i]['type']) {
case 'cdata':
array_push($chi ld, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
if(!empty($name )){
$child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
if(isset($value s[$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_arr ay($values, $i);
break;
case 'close':
return $child;
break;
}
}
return $child;
}//_struct_to_arra y
/**
* createArray($da ta)
*
* 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()
{
$xml = $this->xml;
$values = array();
$index = array();
$array = array();
$parser = xml_parser_crea te();
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, $xml, $values, $index);
xml_parser_free ($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
$array[$name] = $this->_struct_to_arr ay($values, $i);
return $array;
}//createArray
}//XmlToArray
?>
[/PHP]
However, this is just way too deep (I think) once completed for it to be usable for this project.
(did any of that make sense?)
I would be more than willing to provide a full sample return xml string via email if some one needs to see just how it is being sent to me…
Thank you for your time.