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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nine72
    New Member
    • Oct 2006
    • 21

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

    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.

    Code:
    	<Response>
                       <insert_status>Accepted</insert_status>
    	</Response>.
    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..


    Code:
    <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>
    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 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.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by nine72
    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.xm l"; // $xml = $xmlInputString ;
    $xsl = "ControlToWDDX. xsl";
    $result = XSLTransform->process($xml , $xsl); // php5 xslt processing
    $array = wddx_deserializ e($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

    Comment

    Working...