Ok, I am not sure where to start with this or even if this is the right forum topic (could be in the PHP section also..), I can generally figure out my issues after a few days…but not this time…haha.
Overview:
Receive an incoming XML string, parse, store to session or other method and present results on a web page (PHP).
Currently, I am generating an xml sting from a set of 15 forms and sending to a middleware app that validates business rules and commits to a central DB. I have not been required to accept a response other than the following…
The RecordId I generate and send and it is just returned to me, The Status I would see if it were Complete or Failed and direct the user to the correct page.
Now I am being required to take the incoming string and return all values to a completed page if the insert is successful.
As stated above the string I send is generated from 15 form pages. However, 3 pages can be “recycled” as many times in a session as the user requires. This is say a Contact page, they can give me 1 contact or a 1K contacts. For each contact that is returned it would look something like this…
So if they enter 100 Contacts then how in the world do I take a tag like <state> that occurs 100 times, parese it and make it unique and then return all the unique tags to some variable that I can then call on a completion page and present the information in a usable format…
I am currently trying to use this class….
And it works well enough; it gives me huge nested arrays of the xml data….but I feel that there has to be a better way to do it…
Thanks….
Overview:
Receive an incoming XML string, parse, store to session or other method and present results on a web page (PHP).
Currently, I am generating an xml sting from a set of 15 forms and sending to a middleware app that validates business rules and commits to a central DB. I have not been required to accept a response other than the following…
Code:
<Response>
<RecordId>Id_Here</RecordId>
<Status>Insert Complete or Failed</Status>
<Response>
The RecordId I generate and send and it is just returned to me, The Status I would see if it were Complete or Failed and direct the user to the correct page.
Now I am being required to take the incoming string and return all values to a completed page if the insert is successful.
As stated above the string I send is generated from 15 form pages. However, 3 pages can be “recycled” as many times in a session as the user requires. This is say a Contact page, they can give me 1 contact or a 1K contacts. For each contact that is returned it would look something like this…
Code:
<Contact>
<contact_id>971</contact_id>
<contact_type>ACCOUNTING</contact_type>
<first_name>JANE</first_name>
<last_name>DOE</last_name>
<middle_i>M</middle_i>
<address1>147 OCEAN DR</address1>
<address2>null</address2>
<state_cd>MA</state_cd>
<city>RIVERMOOR</city>
<zip>01556</zip>
<cntry_cd>840</cntry_cd>
<telephone>3213213211</telephone>
<mobile>null</mobile>
<email_id>NOEMAIL@MYJOB.COM</email_id>
<department>null</department>
<title>null</title>
<fax>5555555555</fax>
<url>null</url>
<country>UNITED STATES OF AMERICA</country>
<state>MASSACHUSETTS</state>
<customer_care_phone>3213213211</customer_care_phone>
</Contact>
So if they enter 100 Contacts then how in the world do I take a tag like <state> that occurs 100 times, parese it and make it unique and then return all the unique tags to some variable that I can then call on a completion page and present the information in a usable format…
I am currently trying to use this class….
Code:
<?
/**
* XMLToArray Generator Class
* Purpose : Creating Hierarchical Array from XML Data
*/
class XmlToArray
{
var $xml='';
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function XmlToArray($xml)
{
$this->xml = $xml;
}
/**
* _struct_to_array($values, &$i)
*
* This is adds the contents of the return xml into the array for 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()
{
$xml = $this->xml;
$values = array();
$index = 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, $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_array($values, $i);
return $array;
}//createArray
}
?>
And it works well enough; it gives me huge nested arrays of the xml data….but I feel that there has to be a better way to do it…
Thanks….