Jump to content

Get XML using simplexml_load_file


jester626

Recommended Posts

I have a simple XML file that I am trying to get PHP to display in a format that is easier to read. I am using the following code to get the xml data

 

<SNIPPET>

if (file_exists('raw.xml')) {

    $xml = simplexml_load_file('raw.xml');

 

 

    print_r($xml);

} else {

    exit('Failed to open raw.xml.');

}

</SNIPPET>

 

and the results page displays the data as such:

 

SimpleXMLElement Object ( [ADN] => 680605000008000 [Date] => 6/6/2007 5:55:00 AM

 

 

This is where I am totally lost. How can I make PHP remove the "SimpleXMLElement Object" text as well as the other extraneous characters so it only displays the actual data (i.e. 680605000008000, 6/6/2007 5:55:00 AM )

 

my end result is to format the display so I will be able to add additional MySQL INSERT syntax for adding to a DB.

 

Thanks for your help.

 

Jester

Link to comment
Share on other sites

maybe try this function to turn the object into an array:

 

<?
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 strval($object); // strval and everything is fine
   }

   return $return;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.