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

search for in the

simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 13 Nov 2009

view this page in

simplexml_load_file

(PHP 5)

simplexml_load_file XMLファイルをパースし、オブジェクトに代入する

説明

object simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns [, bool $is_prefix = false ]]]] )

指定したファイルの中の整形式 XML ドキュメントをオブジェクトに変換します。

パラメータ

filename

XML ファイルへのパス。

注意: Libxml 2 は URI をエスケープしませんので、例えば URI パラメータ ab&c を渡したい場合、 simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))) をしてコールする必要があります。PHP 5.1.0 以降では、これをする必要は ありません。PHP が自動的に行います。

class_name

simplexml_load_file() が指定されたクラスのオブジェクトを返すようにするために、 このオプションのパラメータを使用します。 このクラスは、SimpleXMLElement クラスを継承していなければなりません。

options

PHP 5.1.0 と Libxml 2.6.0 から、追加の Libxml パラメータ を指定するために options を使用することもできます。

ns

is_prefix

返り値

SimpleXMLElement クラスのオブジェクトを返します。 XML ドキュメント内のデータをプロパティに含みます。 エラー時には FALSE を返します。

エラー / 例外

XML データ内でエラーが見つかるたびに E_WARNING エラーメッセージが発生します。

ヒント

libxml_use_internal_errors() ですべての XML エラーを抑制し、 後から libxml_get_errors() で取得することもできます。

例1 XMLドキュメントをパースする

<?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');

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

このスクリプトは成功時に以下のように出力します。

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

この時点で、$xml->title としたり、 他の全ての要素にアクセスすることができます。

参考



simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
simplexml_load_file
knl at bitflop dot com
12-Sep-2009 04:02
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;

?>
neil art neilanddeb dort com
17-Aug-2009 10:00
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
christoph dot burgdorfer at gmail dot com
01-Feb-2009 05:30
This function gets RSS Items of a Wordpress blog from a search and fills the respective elements  into an array:

<?php

function get_rss_items($searchkeywords) {
   
// define url
   
$url = "http://some.wordpress.blog.com/feed/?s=" . urlencode($searchkeywords) . "&submit=";
   
   
// retrieve search results
   
if($xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA)) {
       
$result["title"]   = $xml->xpath("/rss/channel/item/title");
       
$result["link"]    = $xml->xpath("/rss/channel/item/link");
       
$result["content"] = $xml->xpath("/rss/channel/item/content:encoded/text()");

        foreach(
$result as $key => $attribute) {
           
$i=0;
            foreach(
$attribute as $element) {
               
$ret[$i][$key] = (string)$element;
               
$i++;
            }
        }   
        return
$ret;   
    } else
        return
false;   
}

?>
cryonyx at cerebrate dot ru
21-Oct-2008 07:34
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];
  ...;
}
?>
mario
02-Sep-2008 06:08
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);
?>
clarke DOT chris at googley mail DOT com
26-Jul-2008 12:33
For clarification, finding attributes seems easier this way, hope I'm not being redundant.

Source XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body type="small" important="low">Don't forget me this weekend!</body>
</note>

Code Example:
<?php
$xml
= simplexml_load_file("test.xml");
print
$xml->body['type'];
?>

Output:
small
pc_storm at abv dot bg
20-May-2008 09:16
You can avoid easily, the unpredictable destruction of $_SESSION when loading a simple_xml object (which occurs under _some_ PHP versions) by serializing and unserializing it:

<?php
$oldsession
=serialize($_SESSION);
$myxml=@simplexml_load_file('my.xml');
$_SESSION=unserialize($oldsession);
unset (
$oldsession);
?>

This worked for me under PHP 5.2.5 while on 5.1.6 there was no need for such workaround at all.

Same workaround should help if there are other superglobals affected by this bug...
l [DOT] anzinger [AT] gmail [DOT] com
27-Mar-2008 02:03
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); ?>
php at werner dash ott dot de
29-Mar-2007 06:13
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
?>
wouter at code-b dot nl
20-Feb-2007 07: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:

<?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"
Kyle
11-Dec-2006 07: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.
Anonymous
07-Apr-2006 01: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 10: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 01: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;
   }
?>
skutter at imprecision dot net
04-Feb-2006 02: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 08: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 11: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
01-Oct-2005 12: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 03:06
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;
}
?>

simplexml_load_string> <simplexml_import_dom
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites