Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert an XML doc to a string

Status
Not open for further replies.

CliffLandin

Programmer
Nov 14, 2004
81
US
The Background:

I am developing a site that queries a booking agent that returns an XML doc full of booking information. I use file_get_contents to return the data as a string which is then parsed using simplexml_load_string. I had been using simplexml_load_file and passing it the url, but I then found out that there is a problem with simplexml objects being loaded into sessions. So, I was hoping to retrieve the doc as a string which I would then load into the session and be able to parse it at any time without having to query the booking agent again.

The Quandry:

The string that file_get_contents returns is not an element that I can manipulate. The information is there and is parsed by the simplexml_load_string, but if I try to load it into the session and then dump the $_SESSION the variable is empty. On the same note, if I echo the string it shows nothing, but if I view source on the page all the information is there.

Code:
Example of XML visible when viewing the page source.

<?xml version="1.0" encoding="utf-8"?><SearchResults><Errors /><Session ID="0B2A0F05-5B5E-416B-9C5D-6E72DD375375" SalesID="*****" DestinationID="0" Region="" Country="" ArrivalDate="04/09/2007" ArrivalTime="00:00:00" DepartureDate="04/23/2007" DepartureTime="00:00:00" AdultCount="1" ProductCategoryID="1" LengthOfStay="14" DaysUntilArrival="1" Test="0" /><SearchSpeed SearchMarker1="2140" SearchMarker2="3640" SearchEnd="7563" /><Locations>...

The Question:

Is there a way to take this XML doc and convert it to a string that I can then place in the session and then later parse with simplexml_load_string?

The Answer:
?

Thanks for any help that you can provide.

When in doubt, go flat out!

 
Not sure if this helps or not, but I found an example on PHP.net that talks about how to put SimpleXML objects into a session.

In order to store such information in the session, we have to convert all of the SimpleXMLElement objects present in the structure:

Code:
Array
(
    [USERS] => Array
        (
            [0] => Array
                (
                    [NAME] => Joel Fielder
                    [EMAIL] => joelfielder@hotmail.com
                )

        )

)

And here is the code to do so:

Code:
<?php
function object2array($object)
{
    $return = NULL;
       
    if(is_array($object))
    {
        foreach($object as $key => $value)
            $return[$key] = object2array($value);
    }
    else
    {
        $var = get_object_vars($object);
           
        if($var)
        {
            foreach($var as $key => $value)
                $return[$key] = object2array($value);
        }
        else
            return $object;
    }

    return $return;
}
?>

Ron Wheeler
 
i may not be understanding this properly but it seems that Cliff is asking for how to put the xml document into a session variable before parsing it.

this is as simple as the following
Code:
session_start();
$xmlfile = "[URL unfurl="true"]http://www.example.com/path/to/file.xml";[/URL]
$_SESSION['xml'] = file_get_contents($xmlfile);

and to create a simple_xml object
Code:
$obj = simplexml_load_string($_SESSION['xml']);

but ... there are some problems reported with simplexml. is it necessary that you use this over any other construct?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top