Jump to content

Almost done and one last question....


nine72

Recommended Posts

First I want the thank everyone for the fantastic help I have received here. You all have been excellent! ;D

 

I am to the final stages of the project that I am working on, and I have one last issue that I am struggling with…. ???

 

I am getting back an xml string from the middleware app that I post my xml string to. This string is a string and not a file. (I am working with a test "file" at the moment and may need some help reading the actuall sting... ::) )

Anyway the string comes in and I can get it into an associative array so that is good.

What I need some help with is remove some tags that I do not need and then turn this array to a named session array and automatically post to another page.

 

partial example of the parsed xml array as it is now…

 

Array
(
    [xml] => Array
        (
            [Response] => Array
                (
                     => Array                        (
          [ResponseText] => Succeed   
                            [contact_id] => 677
                            [r_contact_type] => DEFAULT
                            [r_first_name] => JOHN
                            [r_last_name] => DOE
                            [r_middle_i] => B
                            [r_address1] => 100 SOME ST
                            [r_address2] => SUITE 180
                            [r_state_cd] => TX
                            [r_city] => DALLAS
                            [r_zip] => 75287
                            [r_cntry_cd] => 840
                            [r_telephone] => 9879879877
                            [r_mobile] => 
                            [r_email_id] => [email protected]
                            [r_department] => 
                            [r_title] => 
                            [r_fax] => 3213213211
                            [r_url] => 
                            [r_country] => UNITED STATES OF AMERICA
                            [r_state] => TEXAS
                            [r_customer_care_phone] => 9879879877
                            [r_copy_to_accounting] => 
                            [r_copy_to_billing] => 
                            [r_copy_to_chargeback] => 
                            [r_copy_to_helpdesk] => 
                            [r_copy_to_management] => 
                            [r_copy_to_technical] => 
                            [r_apply_to_cp] => 
                            [r_apply_to_mm] => 
                            [r_apply_to_me] => 
                            [r_apply_to_tt] => 
                            [contact_type] => ACCOUNTING
                            [first_name] => JANE
                            [last_name] => DOE
                            [middle_i] => M
                            [address2] => 
                            [state_cd] => MA
                            [city] => RIVERMORE
                            [zip] => 01556
                            [cntry_cd] => 840
                            [telephone] => 9879879877
                            [mobile] => 
                            [email_id] => [email protected]
                            [department] => 
                            [title] => 
                            [fax] => 3213213211
                            [url] => 
                            [country] => UNITED STATES OF AMERICA
                            [state] => MASSACHUSETTS
                            [customer_care_phone] => 9879879877
                            [copy_to_accounting] => 
                            [copy_to_billing] => 
                            [copy_to_chargeback] => 
                            [copy_to_helpdesk] => 
                            [copy_to_management] => 
                            [copy_to_technical] => 
                            [apply_to_cp] => 
                            [apply_to_mm] => 
                            [apply_to_me] => 
                            [apply_to_tt] => 
                        )

                )

             => >        )

)

 

Put into a named session of $_SESSION[‘xmlResponse’]['$key'] = $val;

 

And based on [ResponseText] => val

 

Send to ether a “Complete” page or a “Failed” page.

 

This is the parse class that I am using....

<?PHP

/** 
* 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 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
 
?>

 

So any assistance would be out standing!

 

Thanks so much everyone…

 

nine

Link to comment
https://forums.phpfreaks.com/topic/129792-almost-done-and-one-last-question/
Share on other sites

Your question is not put forward very clearly.

 

But judging from what you said here:

Put into a named session of $_SESSION[‘xmlResponse’]['$key'] = $val;

 

And based on [ResponseText] => val

 

Send to ether a “Complete” page or a “Failed” page.

You're wanting to display a page based on what ResponseText is set to within your array which is set in the $_SESSION[‘xmlResponse’] variable. If so you'd do

 

if($_SESSION['xmlResponse']['ResponseText'] == 'Succeed')
{
     // request was successful
}
else
{
     // request was not successful
}

Sorry about not being really clear on some of this wildteen88 but you did answer part of my problem and I thank you.

 

I had it in my mind that it was going to be way more complicated than that…I think because I have not been able to figure out how to access and use [ResponseText] => Succeed as an actual variable that I can use from the parse.

 

The rest of the problem is that I parse the response, but I need to take the results that I get from the parse and move them in to the named session i.e. $_SESSION[‘xmlResponse’]

 

Example

Parse Results – 

Array
(
    [xml] => Array
        (
            [Response] => Array
                (
                     => Array    
    [ResponseText] => Succeed   
                            [contact_id] => 677
                            [r_contact_type] => DEFAULT
                            [r_first_name] => JOHN

Turn into –

$_SESSION[‘xmlResponse’][‘ResponseText’] = “Succeed”;
$_SESSION[‘xmlResponse’][‘contact_id’] = “667”;
$_SESSION[‘xmlResponse’][‘r_contact_type’] = “DEFAULT”;
$_SESSION[‘xmlResponse’][‘r_first_name’] = “JOHN”;

Etc… 

 

Kind of like when I am doing a POST from form page to form page I take the incoming POST and do


foreach ($_POST as $key => $val) { 
     $_SESSION['form_data'][$key] = $val;  
 }

 

Then do as you suggest and access the $_SESSION[‘xmlResponse’] via session_start() on the Complete Page.

 

On the complete page I need to take the $_SESSION[‘xmlResponse’] values and print them in an organized manner for the user to print out, save etc…

 

This is the parse class that I am using....

<?PHP

/** 
* 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 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
 
?>

Okay so the above is the class your use to convert XML to an array

 

To use it you'd do

 

// make a new instance of the XMLtoArray object.
$xml = new XmlToArray('myfile.xml');

// convert the XML to an array
// the createArray method returns an array
$xml_array = $xml->createArray();

// save the generated array to the $_SESSION variable
$_SESSION['xmlResponse'] = $xml_array['xml']['Response'];

wildteen88, I...I...don't know what to say....that is beautiful!

 

I had been printing to the array out to the page using...

 

//Creating Instance of the Class 
$xmlObj    = new XmlToArray($xml_data); 
//Creating Array 
$arrayData = $xmlObj->createArray(); 

 

it is that last line you supplied that I could not figure out...

// save the generated array to the $_SESSION variable
$_SESSION['xmlResponse'] = $xml_array['xml']['Response'];

 

I can not beleive that it is that simple...I must have been over thinking...because nothing about this first phase of this project has been THAT easy for a year now...

 

I think that I shall consider you a php God .... :D

 

I will set this up and run with it...if I can make it all work I can move to phase 2...w00t

 

Thank you again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.