PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 20 Jun 2008

view this page in

simplexml_load_file

(PHP 5)

simplexml_load_file — Convertit un fichier XML en objet

Description

object simplexml_load_file ( string $filename [, string $class_name [, int $options [, string $ns [, bool $is_prefix ]]]] )

Convertit le document XML filename en un objet de type SimpleXMLElement.

Liste de paramètres

filename

Chemin vers le fichier XML

Note: Libxml 2 supprime la protection des caractères des URI, alors si vous voulez passer par exemple b&c comme paramètre URI à a, vous devez appeler simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Depuis PHP 5.1.0, vous n'avez plus besoin de faire cela puisque PHP le fait pour vous.

class_name

Vous pouvez utiliser ce paramètre optionnel et ainsi, la fonction simplexml_load_file() retournera un objet de la classe spécifiée. Cette classe doit étendre la classe SimpleXMLElement.

options

Depuis PHP 5.1.0 et Libxml 2.6.0, vous pouvez aussi utiliser le paramètre options pour spécifier des paramètres additionnels Libxml.

ns

is_prefix

Valeurs de retour

Retourne un objet de la classe SimpleXMLElement dont les propriétés contiennent les données du document XML. Si une erreur survient, la fonction retournera FALSE.

Exemples

Exemple #1 Interprétation d'un document XML

<?php
// Le fichier test.xml contient un document XML avec un élément racine
// et au moins un élément /[racine]/title.

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

    
print_r($xml);
} else {
    exit(
'Echec lors de l\'ouverture du fichier test.xml.');
}
?>

Ce script affichera, en cas de succès :

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

À partir de là, vous pouvez utiliser $xml->title et tout autre élément.



simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
simplexml_load_file
Anonymous
11-Jun-2008 11:01
The comment about libxml unescaping the uri might be slightly wrong, because it mentions that "Since PHP 5.1.0 you don't need to do this because PHP will do it for you." but with PHP 5.2.3 I still have to call rawurlencode() if the filename contains characters that might be unescaped by libxml.
mail at mdijksman dot nl
15-May-2008 03:04
About aleshru at gmail dot com's object2array function:

For me doing

<?php
$result
= (array) $object;
?>

does exactly the same.
wouter at code-b dot nl
20-Feb-2007 11:08
To correctly extract a value from a CDATA just make sure you cast the SimpleXML Element to a string value by using the cast operator:

$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<rss>
    <channel>
        <item>
            <title><![CDATA[Tom & Jerry]]></title>
        </item>
    </channel>
</rss>';

$xml = simplexml_load_string($xml);

// echo does the casting for you
echo $xml->channel->item->title;

// but vardump (or print_r) not!
var_dump($xml->channel->item->title);

// so cast the SimpleXML Element to 'string' solve this issue
var_dump((string) $xml->channel->item->title);

Above will output:

Tom & Jerry

object(SimpleXMLElement)#4 (0) {}

string(11) "Tom & Jerry"
Kyle
10-Dec-2006 11:35
In regards to Anonymous on 7th April 2006

There is a way to get back HTML tags. For example:

<?xml version="1.0"?>
<intro>
    Welcome to <b>Example.com</b>!
</intro>

<?php
// I use @ so that it doesn't spit out content of my XML in an error message if the load fails. The content could be passwords so this is just to be safe.
$xml = @simplexml_load_file('content_intro.xml');
if (
$xml) {
   
// asXML() will keep the HTML tags but it will also keep the parent tag <intro> so I strip them out with a str_replace. You could obviously also use a preg_replace if you have lots of tags.
   
$intro = str_replace(array('<intro>', '</intro>'), '', $xml->asXML());
} else {
   
$error = "Could not load intro XML file.";
}
?>

With this method someone can change the intro in content_intro.xml and ensure that the HTML is well formed and not ruin the whole site design.
info at tpsoftware dot de
16-Aug-2006 11: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 06: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 02: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
06-Feb-2006 05: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
05-Feb-2006 06: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
03-Feb-2006 06: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 12: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 03: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 05: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
12-Sep-2005 08: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 05: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 04: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 09: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>";
?>

simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites