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
https://forums.phpfreaks.com/topic/70750-get-xml-using-simplexml_load_file/
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;
}
?>

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.