simplexml_load_file

(PHP 5)

simplexml_load_file --  Interprets an XML file into an object

Description

object simplexml_load_file ( string filename [, string class_name [, int options]] )

This function will convert the well-formed XML document in the file specified by filename to an object of class SimpleXMLElement. If any errors occur during file access or interpretation, the function returns FALSE.

You may use the optional class_name parameter so that simplexml_load_file() will return an object of the specified class. That class should extend the SimpleXMLElement class.

Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

注: Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.

例子 1. Interpret an XML document

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('test.xml')) {
    
$xml = simplexml_load_file('test.xml');

    
var_dump($xml);
} else {
    exit(
'Failed to open test.xml.');
}
?>

This script will display, on success:

SimpleXMLElement Object
(
  [title] => Example Title
  ...
)

At this point, you can go about using $xml->title and any other elements.

See also: simplexml_load_string()


add a note add a note User Contributed Notes
info at tpsoftware dot de
16-Aug-2006 05:55
The object2array function by joelfielder is very nice but fails for CDATA. Very simple fix:

<?
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;
}
?>
Anonymous
07-Apr-2006 12:21
What has been found when using the script is that simplexml_load_file() will remove any HTML formating inside the XML file, and will also only load so many layers deep. If your XML file is to deap, it will return a boolean false.
fdouteaud at gmail dot com
09-Mar-2006 09:21
Be careful if you are using simplexml data directly to feed your MySQL database using MYSQLi and bind parameters.

The data coming from simplexml are Objects and the bind parameters functions of MySQLi do NOT like that! (it causes some memory leak and can crash Apache/PHP)

In order to do this properly you MUST cast your values to the right type (string, integer...) before passing them to the binding methods of MySQLi.
I did not find that in the documentation and it caused me a lot of headache.
info at evasion dot cc
07-Feb-2006 12:26
Sorry there's a mistake in the previous function :
<?php
  
function &getXMLnode($object, $param) {
       foreach(
$object as $key => $value) {
           if(isset(
$object->$key->$param)) {
               return
$object->$key->$param;
           }
           if(
is_object($object->$key)&&!empty($object->$key)) {
              
$new_obj = $object->$key;
              
// Must use getXMLnode function there (recursive)
              
$ret = getXMLnode($new_obj, $param); 

           }
       }
       if(
$ret) return (string) $ret;
       return
false;
   }
?>
Bart Verkoeijen
06-Feb-2006 01:30
No problems at all with CDATA:

Test.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<xml>
   <cdata><![CDATA[abc<br>abc]]></cdata>
</xml>

PHP Code:
<?php
$xml
= simplexml_load_file( 'test.xml' );
echo
$xml->cdata;
?>

Output:
> X-Powered-By: PHP/5.1.1
> Content-type: text/html
>
> abc<br>abc

The problem below is probably caused by incorrect syntax. It's "<![CDATA[]]>" instead of "<![CDATA []]>" (note the space).
skutter at imprecision dot net
04-Feb-2006 01:11
So it seems SimpleXML doesn't support CDATA... I bashed together this little regex function to sort out the CDATA before trying to parse XML with the likes of simplexml_load_file / simplexml_load_string. Hope it might help somebody and would be very interested to hear of better solutions. (Other than *not* using SimpleXML of course! ;)

It looks for any <![CDATA [Text and HTML etc in here]]> elements, htmlspecialchar()'s the encapsulated data and then strips the "<![CDATA [" and "]]>" tags out.

<?php
function simplexml_unCDATAise($xml) {
  
$new_xml = NULL;
  
preg_match_all("/\<\!\[CDATA \[(.*)\]\]\>/U", $xml, $args);

   if (
is_array($args)) {
       if (isset(
$args[0]) && isset($args[1])) {
          
$new_xml = $xml;
           for (
$i=0; $i<count($args[0]); $i++) {
              
$old_text = $args[0][$i];
              
$new_text = htmlspecialchars($args[1][$i]);
              
$new_xml = str_replace($old_text, $new_text, $new_xml);
           }
       }
   }

   return
$new_xml;
}

//Usage:
$xml = 'Your XML with CDATA...';
$xml = simplexml_unCDATAise($xml);
$xml_object = simplexml_load_string($xml);
?>
info at evasion dot cc
03-Feb-2006 07:37
Suppose you have loaded a XML file into $simpleXML_obj.
The structure is like below :

SimpleXMLElement Object
(

   [node1] => SimpleXMLElement Object
       (
           [subnode1] => value1
           [subnode2] => value2
           [subnode3] => value3
       )

   [node2] => SimpleXMLElement Object
       (
           [subnode4] => value4
           [subnode5] => value5
           [subnode6] => value6
       )

)

When searching a specific node in the object, you may use this function :
      
<?php

  
function &getXMLnode($object, $param) {
       foreach(
$object as $key => $value) {
           if(isset(
$object->$key->$param)) {
               return
$object->$key->$param;
           }
           if(
is_object($object->$key)&&!empty($object->$key)) {
              
$new_obj = $object->$key;
              
$ret = getCfgParam($new_obj, $param);   
           }
       }
       if(
$ret) return (string) $ret;
       return
false;
   }
?>

So if you want to get subnode4 value you may use this function like this :

<?php
$result
= getXMLnode($simpleXML_obj, 'subnode4');
echo
$result;
?>

It display "value4"
patrick at procurios dot nl
12-Jan-2006 10:46
simplexml_load_file creates an xml-tree with values that are UTF-8 strings. To convert them to the more common encoding 
ISO-8859-1 (Latin-1), use "utf8_decode".
genialbrainmachine at NOSPAM dot tiscali dot it
30-Sep-2005 11:52
Micro$oft Word uses non-standard characters and they create problems in using simplexml_load_file.
Many systems include non-standard Word character in their implementation of ISO-8859-1. So an XML document containing that characters can appear well-formed (i.e.) to many browsers. But if you try to load this kind of documents with simplexml_load_file you'll have a little bunch of troubles..
I believe that this is exactly the same question discussed in htmlentites. Following notes to htmlentitles are interesting here too (given in the reverse order, to grant the history):
http://it.php.net/manual/en/function.htmlentities.php#26379
http://it.php.net/manual/en/function.htmlentities.php#41152
http://it.php.net/manual/en/function.htmlentities.php#42126
http://it.php.net/manual/en/function.htmlentities.php#42511
mark
13-Sep-2005 02:06
If the property of an object is empty the array is not created. Here is a version object2array that transfers properly.

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] = ($key && !$value) ? NULL : object2array($value);
       }
       else return $object;
   }

   return $return;
}
joelfielder at hotmail dot com
19-Jul-2005 11:01
In the object2array function posted above, the following data structure would be left unchanged:

Array
(
   [0] => Object Object
       (
           [var] => 1
       )

)

The simplexml_load_... functions return structures similar to the above, a simple example:

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

       )

)

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

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

       )

)

And here is the code to do so:

<?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;
}
?>
pa ul at sant a soft dot co m
12-Jul-2005 10:34
One thing to note about the object2array function from aleshru below...  this function doesn't handle CDATA fields (e.g. a poorly formed HTML formatted message.)  It just returns an empty array.

example XML:
--------------8<---------------
<note>
<subject>HTML Message Dude!</subject>
<content><![CDATA[ <B> this is some </b> CDATA data that is useful to handle or at
least see. <div> it doesn't matter that the HTML isn't properly </di>v opened or closed.

<br> the html content is still continuing!!]]></content>
</note>
--------------8<---------------

the simpleXML built in functionality seems to handle this just dandily however.

<?php

$object
= simplexml_load_string($that_string_of_xml_above);
echo ((string)
$object->content);

/*  OUTPUT:
 <B> this is some </b> CDATA data that is useful to handle or at
least see. <div> it doesn't matter that the HTML isn't properly </di>v opened or closed.

<br> the html content is still continuing!!
*/
?>

This object2array function does work well for the other types of xml content however.
aleshru at gmail dot com
28-May-2005 03:16
I've got function to convert SimpleXmlObject's to array.
<?php

function object2array ( $object )
{
   if ( !
is_object ( $object ) )
     return
$object;

  
$return = array ();

  
$var = get_object_vars ( $object );

   while ( list (
$k, $v ) = each ( $var ) )
    
$return [ $k ] = object2array ( $v );
   return
$return;
}

  class
dummy{
     var
$a = 1;
     var
$b = 2;
     var
$c = 3;
       function
__construct(){
          
$this->d = new dummy2();
       }
  }   

  class
dummy2{
       function
__construct(){
                
$this->e = 'f';
                
$this->true = true;
                
$this->false = false;
                
$this->null = null;
                
$this->file = __FILE__;
       }
  }

 
$object = new dummy;
 
$arr = object2array($object);

  echo
"<pre>";
 
print_r($arr);
  echo
"</pre>";
?>