By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Not new to xml generating but am new to really complex parsing...help.

nine72
P: 21
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance…

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
  1.     <Response>
  2.                    <insert_status>Accepted</insert_status>
  3.     </Response>.
  4.  
And I would return the user to a "Complete" or "Insert Faild" page based on the <insert_status>.

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
  1. <Contacts>
  2. <Contact>
  3.  <contact_id>833</contact_id>
  4.  <contact_type>ACCOUNTING</contact_type>
  5.  <first_name>NATALIE</first_name>
  6.  <last_name>GEWN</last_name>
  7.  <middle_i>M</middle_i>
  8.  <address1>147 OCEAN DR</address1>
  9.  <address2></address2>
  10.  <state_cd>MA</state_cd>
  11.  <city>RIVERMORE</city>
  12.  <zip>01556</zip>
  13.  <cntry_cd>840</cntry_cd>
  14.  <telephone>9879879877</telephone>
  15.  <mobile></mobile>
  16.  <email_id>NGEWN@MYCO.COM</email_id>
  17.  <department></department>
  18.  <title></title>
  19.  <fax>3213213211</fax>
  20.  <url></url>
  21.  <country>UNITED STATES OF AMERICA</country>
  22.  <state>MASSACHUSETTS</state>
  23.  <customer_care_phone>9879879877</customer_care_phone>
  24. </Contact>
  25. </Contacts>
  26.  
50 times how would I be able make each Contact and its sub set of tags unique so that I could store them in the PHP $_SESSION array where I can pull the distinct values to populate a response page giving back the information as it is returned to me. One reason that I would need to put it to a session is that some returned response would be meaningless to the user such as <billing_value>00MER90</billing_value>. I would have to search 00MER90 and print to the page its corresponding friendly name such as “Mileage Earned Revenue”


Right now I can return the string and print the array in a multidimensional 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_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()
{
$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


}//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.
Oct 22 '08 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance….
Deity bless you.

now back to "business". if I understand it right (it was quite some text to read) you look for an easy way to transform the middle-ware xml into a session saveable array? (correct me if I'm completely on the wrong track...)

(*evil grin* ahem) as fortune plays I do something similar (but admittedly not that large), I convert a control xml into page specific objects. my solution is to convert it into an xml serialized string that is then deserialized into my desired object.

like that:
[PHP]$xml = "control.xml"; // $xml = $xmlInputString;
$xsl = "ControlToWDDX.xsl";
$result = XSLTransform->process($xml, $xsl); // php5 xslt processing
$array = wddx_deserialize($result);
// do whatever is to be done with $array[/PHP]
advantage, when doing xslt you can convert 00MER90 to Mileage Earned Revenue while you're at it, or crop/trim/replace/alter/add information, as you desire.

regards
Oct 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.