simplexml_load_file

(PHP 5, PHP 7, PHP 8)

simplexml_load_fileÜbersetzt eine XML-Datei in ein Objekt

Beschreibung

simplexml_load_file(
    string $filename,
    ?string $class_name = SimpleXMLElement::class,
    int $options = 0,
    string $namespace_or_prefix = "",
    bool $is_prefix = false
): SimpleXMLElement|false

Die Funktion wandelt das übergebene wohlgeformte (well-formed) XML-Dokument in ein Objekt um.

Parameter-Liste

filename

Der Pfad zur XML-Datei.

class_name

Der optionale Parameter class_name kann verwendet werden, wenn simplexml_load_file() ein Objekt der spezifischen Klasse zurückgeben soll. Die gewählte Klasse sollte von der Klasse SimpleXMLElement abgeleitet sein.

options

Bitweises OR von libxml-Konstanten für weitere Optionen.

namespace_or_prefix

Ein Namensraum-Präfix oder URI.

is_prefix

true, wenn namespace_or_prefix ein Präfix ist, false, wenn er ein URI ist. Der Standardwert ist false.

Rückgabewerte

Gibt ein Objekt der Klasse SimpleXMLElement zurück, dessen Eigenschaften die Daten des XML-Dokuments enthalten. Bei einem Fehler wird false zurückgegeben.

Warnung

Diese Funktion kann sowohl das boolsche false zurückliefern, als auch einen nicht-boolschen Wert, welcher zu false ausgewertet wird. Weitere Informationen entnehmen Sie bitte dem Abschnitt über die boolschen Typen. Benutzen Sie deshalb den === Operator, um den Rückgabewert dieser Funktion zu überprüfen.

Fehler/Exceptions

Generiert eine Fehlermeldung vom Typ E_WARNING für jeden in den XML-Daten gefundenen Fehler.

Tipp

libxml_use_internal_errors() kann hier verwendet werden, um alle XML-Fehlermeldungen auszublenden, und libxml_get_errors(), um danach darüber zu iterieren.

Beispiele

Beispiel #1 Ein XML-Dokument auswerten

<?php
// Die Datei test.xml enthält ein XML-Dokument mit einem Wurzel-Element
// und mindestens einem Element /[root]/title.

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

print_r($xml);
} else {
exit(
'Konnte test.xml nicht öffnen.');
}
?>

Das Skript gibt nach erfolgreichem Laden folgendes aus:

SimpleXMLElement Object
(
  [title] => Beispiel-Titel
  ...
)

Ab diesem Punkt können $xml->title und andere Elemente verwendet werden.

Siehe auch

add a note add a note

User Contributed Notes 31 notes

up
61
ricardo at ricardomartins dot info
12 years ago
Sometimes we have xml's with hyphens nodes, like

<my_xml>
<some-node>value</some-node>
</my_xml>

You'll need to use
<?php
$simpleXmlObj
->{'some-node'}
?>

instead of
<?php
$simpleXmlObj
->some-node;
?>
up
33
wouter at code-b dot nl
17 years ago
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:

<?php
$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"
up
10
sirgrayjn at gmail dot com
8 years ago
// Be carefull if you migrate or use local machine
// for test/development.

// Windows directory separators: "\" and "/"
// You may mix separators "C:\somedir\www/img/bg.jpg".

// Mixed separators path work fine in other functions
// But simplexml_load_file() failed with mixed separators.

// Examples:
include("C:\dir\my.php");    // work (windows)
include("C:\dir/my.php");    // work (windows) with mixed
include("C:/dir/my.php");    // work (windows, linux)
simplexml_load_file("C:\dir\my.php");    // work
simplexml_load_file("C:\dir/my.php");    // failed with mixed
simplexml_load_file("C:/dir/my.php");    // work
up
6
neil art neilanddeb dort com
15 years ago
Because the encoding of my XML file is UTF-8 and the
encoding of my web page is iso-8859-1 I was getting strange characters such as ’ instead of a right single quote.

The solution to this turned out to be hard to find, but really easy to implement.

http://uk3.php.net/manual/en/function.iconv.php

Using the iconv() function you can convert from one encodign to another, the TRANSLIT option seems to work best for what I needed.  Here's my example:

<?php
// convert string from utf-8 to iso8859-1
$horoscope = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $horoscope );
?>

I found the solution on this page...
http://tinyurl.com/lm39xc
Hope this helps
up
1
visualmind at nospam dot php dot net
2 years ago
This may sometimes get missed, but if your xml nodes are in the format:
<prefix:element />

You need to make sure to set the [namespace or prefix] argument and the [isPrefix] argument to true. Also when recalling elements you need to avoid adding the prefix if that is already set, so in the above example "prefix:element" should be added as "element" but when saved the prefix will be added automatically.

If prefix is not set during load or object construction, load will fail to get the nodes correctly and you will not be able to recall elements directly so $xml->{'prefix:element'} will not work either.
up
4
raduispas at gmail dot com
14 years ago
if you want to check when this function fails,make sure to compare the return value with ===  instead of == :

<?php
$url
= 'http://www.example.com';
$xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);
if(
$xml ===  FALSE)
{
  
//deal with error
}
else {
//do stuff }
?>

Otherwise you may end up with FALSE all the time even if the document is ok. Hope this helps someone ;)
up
2
mario
16 years ago
If you want CDATA in your object you should use LIBXML_NOCDATA

<?php
$xml
= simplexml_load_file($file_xml, 'SimpleXMLElement',LIBXML_NOCDATA);
   
   
print_r($xml);
?>
up
1
Rich
8 years ago
I stumbled on this: a single element with a simple string in it becomes a string, but a single element with a *space* in it becomes an Array, with one element, the string space.

I'm sure to XML mystics this is wise and wonderful but it really confused me,  and I thought it might confuse others.

<?php
$parsed
= simplexml_load_string('<container><space> </space><blank></blank><string>hello</string></container>');
$content = json_decode(json_encode($parsed),TRUE);
var_dump($content);
/* Output is:
array(3) {
  'space' => array(1) {           ← did NOT expect this!
    [0] => string(1) " "
  }
  'blank' => array(0) { }
  'string' => string(5) "hello"
}
*/
up
1
siraic at gmail dot com
1 year ago
LibXML typically uses ten times the file size in memory to read a file, and this memory usage falls largely outside the memory limit guarded by PHP.
up
1
tg at debian dot org
2 years ago
This function does not accept all pathnames, in spite of its documentation.

$ php -r 'print_r( simplexml_load_file("%25.xml"));'
PHP Warning:  simplexml_load_file(): I/O warning : failed to load external entity "%25.xml" in Command line code on line 1
up
0
kannan at 99deals dot in
12 years ago
If you have some nodes which are having special characters, it would not load properly

for an instance see the nodes below
<node:number>1538-7445</node:number>
<node:coverDisplayDate>Sep  1 2012 12:00:00:000AM</node:coverDisplayDate>

either you have to change the : to other special characters like '-' in order to convert it properly

Correct Node
<node-number>1538-7445</node-number>
<node-coverDisplayDate>Sep  1 2012 12:00:00:000AM</node-coverDisplayDate>

I have wasted my precious time while debugging this. Please aware about this. ?
up
0
l [DOT] anzinger [AT] gmail [DOT] com
16 years ago
If you don't want that the CDATA values get escaped, just load the XML with LIBXML_NOCDATA as an 3rd argument.

Note: A PHP version >= 5.1.0 is required for this to work.

Example:

<?php simplexml_load_file('xmldatei.xml', null, LIBXML_NOCDATA); ?>
up
0
info at evasion dot cc
18 years ago
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"
up
-1
fdouteaud at gmail dot com
18 years ago
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.
up
-1
tuxedobob
9 years ago
Occasionally you may try to load a file and have it complain about an entity and throw a parser error.

If this is the case, check to make sure that the file in question does not contain an ampersand (&) without a corresponding entity reference.

If it does, or if you want to err on the side of caution, then instead of using simplexml_load_file, try this:

$file = file_get_contents('stuff.xml');
$temp = preg_replace('/&(?!(quot|amp|pos|lt|gt);)/', '&amp;', $file);
$xml = simplexml_load_string($temp) or die("xml not loading");

Read the file into a string, add 'amp;' after any '&' that is not part of a character entity, then parse the string as xml.
up
-2
fusionstream at gmail dot com
12 years ago
If you are loading many files, this may slow down your page load time.

To set a timeout, use file_get_context and then simplexml_load_string

<?php

$fp
= fopen('http://www.example.com/rss', false, stream_create_context(array('http' => array('timeout', '1.5'))));

if (
$fp) {
   
print_r( simplexml_load_string($fp) );
} else {
    echo
"The request timed out";
}
?>
up
-1
php at werner dash ott dot de
17 years ago
Making SimpleXMLElement objects session save.

Besides the effect of not surviving sessions, the SimpleXMLElement object may even crash the session_start() function when trying to re-enter the session!

To come up with a solution for this, I used a pattern as follows. The core idea is to transform the SimpleXMLElement between session calls to and from a string representation which of course is session save.

<?php
 
//
  // session save handling of SimpleXMLElement objects
  // (applies to/ tested with PHP 5.1.5 and PHP 5.2.1)
  // The myClass pattern allows for conveniently accessing
  // XML structures while being session save
  //
 
class myClass
 
{
    private
$o_XMLconfig = null;
    private
$s_XMLconfig = '';
   
    public function
__construct($args_configfile)
    {
     
$this->o_XMLconfig = simplexml_load_file($args_configfile);
     
$this->s_XMLconfig = $this->o_XMLconfig->asXML();
    }
// __construct()
   
   
public function __destruct()
    {
     
$this->s_XMLconfig = $this->o_XMLconfig->asXML();
      unset(
$this->o_XMLconfig); // this object would otherwise crash
                                 // the subsequent call of
                                 // session_start()!
   
} // __destruct()
   
   
public function __wakeup()
    {
     
$this->o_XMLconfig = simplexml_load_string($this->s_XMLconfig);
    }
// __wakeup()
   
 
} // class myClass
?>
up
-2
jamie at splooshmedia dot co dot uk
14 years ago
A wrapper around simplexml_load_file to circumvent nasty error messages when the xml server times out or gives a 500 error etc.

<?php
function loadXML2($domain, $path, $timeout = 30) {

   
/*
        Usage:
       
        $xml = loadXML2("127.0.0.1", "/path/to/xml/server.php?code=do_something");
        if($xml) {
            // xml doc loaded
        } else {
            // failed. show friendly error message.
        }
    */

   
$fp = fsockopen($domain, 80, $errno, $errstr, $timeout);
    if(
$fp) {
       
// make request
       
$out = "GET $path HTTP/1.1\r\n";
       
$out .= "Host: $domain\r\n";
       
$out .= "Connection: Close\r\n\r\n";
       
fwrite($fp, $out);
       
       
// get response
       
$resp = "";
        while (!
feof($fp)) {
           
$resp .= fgets($fp, 128);
        }
       
fclose($fp);
       
// check status is 200
       
$status_regex = "/HTTP\/1\.\d\s(\d+)/";
        if(
preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {   
           
// load xml as object
           
$parts = explode("\r\n\r\n", $resp);   
            return
simplexml_load_string($parts[1]);               
        }
    }
    return
false;
   
}
?>
up
-3
sean at aliencreations dot com
13 years ago
If you find that you are receiving 500 errors with simplexml_load_file() but you can access the xml/rss feed manually through a browser, your script is probably being blocked by a user agent sniffer.

Add this code before your xml call to remedy this issue

<?php

ini_set
("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 0);
ini_set("memory_limit", "10000M");

$rss = simplexml_load_file($feed_url);

?>
up
-2
cryonyx at cerebrate dot ru
16 years ago
In case you have a XML file with a series of equally named elements on one level simplexml incorrectly processes them and doesn't allow to walk through the array using foreach(). As far as I'm concerned, it is the problem caused by PHP xml_parser (see: http://ru2.php.net/manual/ru/function.xml-parser-create.php#53188).

To avoid this, just use count() and walk through the array using for().

Example:

<params>
  <param>
    <name>version.shell</name>
    <value>1.0</value>
  </param>
  <param>
      <name>version.core</name>
      <value>1.0</value>
  </param>
  <param>
      <name>file.lang</name>
      <value>vc.lang</value>
  </param>
  ...
</params>

<?php
$filename
= '...';
$xml = simplexml_load_file($filename);
$p_cnt = count($xml->param);
for(
$i = 0; $i < $p_cnt; $i++) {
 
$param = $xml->param[$i];
  ...;
}
?>
up
-1
genialbrainmachine at NOSPAM dot tiscali dot it
19 years ago
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
up
-2
Kyle
17 years ago
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.
up
-4
skutter at imprecision dot net
18 years ago
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);
?>
up
-4
Nanuit at ossi dot at
13 years ago
A little function very helpfull in using simplexml_load_file behind a proxy

<?php
function getXMLfromURL($url) {
     
$Proxy = getenv("HTTP_PROXY");

      if (
strlen($Proxy) > 1) {
       
$r_default_context = stream_context_get_default ( array
                    (
'http' => array(
                       
'proxy' => $Proxy,
                       
'request_fulluri' => True,
                    ),
                )
            );
       
libxml_set_streams_context($r_default_context);
      }
     
$daten = simplexml_load_file($url);
      return (
$daten);
    }
?>

where HTTP_PROXY is set to e.g.: tcp://proxy:8080
up
-2
rex111 at bigmir dot net
8 years ago
Get all tags and their values. (recursive)

<?php
$xml
= simplexml_load_file('settings.xml');

function
all_tag($xml){
   
$i=0; $name = "";
    foreach (
$xml as $k){
       
$tag = $k->getName();
       
$tag_value = $xml->$tag;
        if (
$name == $tag){ $i++;    }
               
$name = $tag;    
            echo
$tag .' '.$tag_value[$i].'<br />';
       
// recursive
          
all_tag($xml->$tag->children());
    }
}

all_tag($xml);
?>
up
-3
Smokey
14 years ago
for nested and same name values i'v made up this little bit for getting and displaying multiable values from google's geocode when a exact match is not found it returns all close matches in the following format(this is an abriged version of there output)

<Response>
  <Placemark id="1">
    <address> New York 24, NY, USA</address>
    <AddressDetails>
      ..................
    </AddressDetails>
    <Point>
      <coordinates>-73.5850086,40.7207442,0</coordinates>
    </Point>
  </Placemark>
  <Placemark id="2">
    <address>New York 27, NY, USA</address>
    <AddressDetails>
      ...................
    </AddressDetails>
    <Point>
      <coordinates>-72.8987835,40.8003588,0</coordinates>
    </Point>
  </Placemark>
  <Placemark id="3">
    <address>Cedar Place School, 20 Cedar Pl, Yonkers, NY 10705, USA</address>
    <AddressDetails>
      ..................
    </AddressDetails>
    <Point>
      <coordinates>-73.8966320,40.9256520,0</coordinates>
    </Point>
  </Placemark>
</Response>

<?php
// get and breakdown the results then store them in $var's
$Address = "99999 parkplace, new york, NY";
$urladdress = urlencode($Address);
$Base_url = "http://maps.google.com/maps/geo?q=";
$urlParts = "&output=xml";
$urlrequest = $Base_url . $urladdress . $urlParts;
$xml = simplexml_load_file($urlrequest);
$num = "0";
foreach (
$xml->Response->Placemark as $value){
   
$num++;
   
$GeoFindAdd{$num} = $value->address;
   
$GeoFindCords{$num} = $value->Point->coordinates;
}

// a simple display for the results
echo "Found ",$num," Possable Geo Data Sets <br>";
$CountNumResults = "0";
for ( ;
$num > 0; $num--){
   
$CountNumResults++;
    echo
$countnum,"<br> Address = ",$GeoFindAdd{$num},"<br> Coordinates = ",$GeoFindCords{$num},"<br>";
}
echo
"END";
?>
up
-3
info at evasion dot cc
18 years ago
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;
   }
?>
up
-5
mark
19 years ago
If the property of an object is empty the array is not created. Here is a version object2array that transfers properly.

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

    return
$return;
}
?>
up
-13
guego dot ol at ig dot com dot br
14 years ago
Analyze fully XML.

<?php
$xml
= simplexml_load_file('file.xml');

foreach(
$xml as $key0 => $value){
echo
"..1..[$key0] => $value";
foreach(
$value->attributes() as $attributeskey0 => $attributesvalue1){
echo
"________[$attributeskey0] = $attributesvalue1";
}
echo
'<br />';
////////////////////////////////////////////////
foreach($value as $key => $value2){
echo
"....2.....[$key] => $value2";
foreach(
$value2->attributes() as $attributeskey => $attributesvalue2){
echo
"________[$attributeskey] = $attributesvalue2";
}
echo
'<br />';
////////////////////////////////////////////////
foreach($value2 as $key2 => $value3){
echo
".........3..........[$key2] => $value3";
foreach(
$value3->attributes() as $attributeskey2 => $attributesvalue3){
echo
"________[$attributeskey2] = $attributesvalue3";
}
echo
'<br />';
////////////////////////////////////////////////
foreach($value3 as $key3 => $value4){
echo
"...................4....................[$key3] => $value4";
foreach(
$value4->attributes() as $attributeskey3 => $attributesvalue4){
echo
"________[$attributeskey3] = $attributesvalue4";
}
echo
'<br />';
////////////////////////////////////////////////
foreach($value4 as $key4 => $value5){
echo
".....................5......................[$key4] => $value5";
foreach(
$value5->attributes() as $attributeskey4 => $attributesvalue5){
echo
"________[$attributeskey4] = $attributesvalue5";
}
echo
'<br />';
////////////////////////////////////////////////
foreach($value5 as $key5 => $value6){
echo
"......................6.......................[$key5] => $value6";
foreach(
$value6->attributes() as $attributeskey5 => $attributesvalue6){
echo
"________[$attributeskey5] = $attributesvalue6";
}
echo
'<br />';
}}}}}
echo
'<br />';
}
?>
up
-4
knl at bitflop dot com
15 years ago
If you need to parse the data from SimpleXML into a session variable remember to define the data as a string first.

If you don't you will get warnings of "Node no longer exists" pointing to your session_start() function.

This will work:

<?php

    $new_version
= simplexml_load_file('http://example.com/version.xml');
   
$_SESSION['current_version'] = (string)$new_version->version;

?>
up
-4
Anonymous
18 years ago
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.
To Top