Hi.
I'm taking my first steps with xml and SimpleXML functions.
xml.php
(It could be made dynamically with a mysql result
I post this snippet for example )

<?php
$xmlstr = "<?xml version=\"1.0\" standalone=\"yes\"?>";
$xmlstr .= "<movies>\n<movie>Behind the Parser</movie>\n</movies>\n";
header('Content-type: text/xml; charset=utf-8');
echo $xmlstr;
?> 

and the loadXml.php

<?php
if (file_exists('xml.php')) {
   $xml = simplexml_load_file('xml.php');

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

I've got this nasty error :

Warning: xml.php:2: parser error : Start tag expected, '<' not found in f:\wamp\www\xml\example\mioplot.php on line 3

Warning: $xmlstr = ""; in c:\xxx\www\xml\example\loadXml.php on line 3

Warning: ^ in c:\xxx\www\xml\example\loadXml.php on line 3
bool(false)

xml.php is a valid xml file, isn't it ?
Therefore I'm wondering why the snippet
doesn't work ?
Thanks in advance.

Bye.

    A file read from the local file system will not run through the PHP parser -- you'll just read the full source code. Try using a URL reference to it, instead, and see if that helps:

    $xml = simplexml_load_file('http://localhost/xml.php');
    

    (Adjust the URL path as needed if xml.php is not in the document root directory.)

      Thanks so much for the ready replay.
      I've got this:

      Warning: simplexml_load_file(http://localhost/test/xmlLoad/xml.php) [function.simplexml-load-file]: failed to open stream: Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato. in f:\wamp\www\yasin\PHP5\tutorials\php\simplexml\xmlLoad\loadPhp.php on line 3

      Warning: http://localhost/test/xmlLoad/xml.php:1: parser error : Blank needed here in f:\wamp\www\yasin\PHP5\tutorials\php\simplexml\xmlLoad\loadPhp.php on line 3

      Warning: in f:\xxx\test\xmlLoad\loadPhp.php on line 3

      Warning: ^ in f:\xxx\test\xmlLoad\loadPhp.php on line 3

      Fatal error: Maximum execution time of 30 seconds exceeded in f:\wamp\xxx\test\xmlLoad\loadPhp.php on line 3

      Excuse me, for the italian 😉

      the loadPhp file:

      <?php
      if (file_exists('xml.php')) {
         $xml = simplexml_load_file('http://localhost/test/xmlLoad/xml.php');
      
         var_dump($xml);
      } else {
         exit('Failed to open test.xml.');
      }
      ?>
      
      

      Thanks again for your help.

      Bye.

        I also tried to put it
        in my host I've got the
        same error :

        Warning: simplexml_load_file() [function.simplexml-load-file]: xml.php:2: parser error : Start tag expected, '<' not found in /mounted-storage/home19a/sub001/sc19485-LOWM/www/xmlLoad/loadPhp.php5 on line 3

        Warning: simplexml_load_file() [function.simplexml-load-file]: $xmlstr = "<?xml version=\"1.0\" standalone=\"yes\"?>"; in /mounted-storage/home19a/sub001/sc19485-LOWM/www/xmlLoad/loadPhp.php5 on line 3

        Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /mounted-storage/home19a/sub001/sc19485-LOWM/www/xmlLoad/loadPhp.php5 on line 3
        bool(false)

        😕

        If I try with a xml file no troubles
        both in my localhost and in my host:

        
        <?php
        if (file_exists('articles.xml')) {
           $xml = simplexml_load_file('articles.xml');
        
           var_dump($xml);
        } else {
           exit('Failed to open test.xml.');
        }
        ?>
        
        Bye.
        
          7 days later

          What is the result of

          if (file_exists('xml.php')) {
             $xml = get_file_contents('http://localhost/test/xmlLoad/xml.php');
          
             var_dump($xml);
          } else {
             exit('Failed to open test.xml.');
          } 

          ?

            Weedpacket wrote:

            What is the result of

            if (file_exists('xml.php')) {
               $xml = get_file_contents('http://localhost/test/xmlLoad/xml.php');
            
               var_dump($xml);
            } else {
               exit('Failed to open test.xml.');
            } 

            ?

            Fatal error: Maximum execution time of 30 seconds exceeded in f:\xmlLoad\loadPhp.php on line 11

            In my php.ini
            allow_url_fopen = On

            It works in my host.

            But if I try with:

            <?php
            if (file_exists('xml.php')) {
               $string= file_get_contents('http://www.blogial.net/test/xml.php');
               $xml = simplexml_load_file($string);
            
               var_dump($xml);
            } else {
               exit('Failed to open test.xml.');
            }
            ?>
            

            I've got:

            Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "%3C%3Fxml%20version=%221.0%22%20standalone=%22yes%22%3F%3E%3Cmovies%3E%0A%3Cmovie%3EBehind%20the%20Parser%3C/movie%3E%0A%3C/movies%3E%0A%20%0D%0A" in /mounted-storage/home/sub/sc/www/test/loadPhp.php on line 4
            bool(false)

            Bye.

              $string= file_get_contents('http://www.blogial.net/test/xml.php');
                 $xml = simplexml_load_file($string); 

              That's because you're retrieving XML from the remote server, and trying to use the XML as a file name.

                Weedpacket wrote:
                $string= file_get_contents('http://www.blogial.net/test/xml.php');
                   $xml = simplexml_load_file($string); 

                That's because you're retrieving XML from the remote server, and trying to use the XML as a file name.

                :o

                <?php
                if (file_exists('xml.php')) {
                $xml = simplexml_load_string(file_get_contents('http://www.blogial.net/test/xml.php'));

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

                Thanks you buddy now it works.

                PS.
                I'm wondering why this does't work :

                Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 2: parser error : Start tag expected, '<' not found in /www/test/loadPhp.php on line 5
                
                Warning: simplexml_load_string() [function.simplexml-load-string]: $xmlstr="<?xml version=\"1.0\" standalone=\"yes\"?>"; in /www/test/loadPhp.php  on line 5
                

                😕

                  As the error message says, there is no start tag in your XML string. Not even the '<' that starts the start tag. The only thing that is there is the XML declaration.

                    Sorry in the last post I made a mistake.
                    This works:

                    <?php
                    if (file_exists('xml.php')) {
                       $xml = simplexml_load_string(file_get_contents('http://www.blogial.net/ua/xml.php'));
                    
                       var_dump($xml);
                    } else {
                       exit('Failed to open test.xml.');
                    }
                    ?>
                    

                    This doesn't work:

                    <?php
                    if (file_exists('xml.php')) {
                       $xml = simplexml_load_string(file_get_contents('xml.php'));
                    
                       var_dump($xml);
                    } else {
                       exit('Failed to open test.xml.');
                    }
                    ?>
                    

                    I can't see the different.
                    Could you explain me ?

                    Thanks in advance.

                    Bye.

                      Try comparing the results of these:

                      <?php
                      if (file_exists('xml.php')) {
                         $xml = file_get_contents('http://www.blogial.net/ua/xml.php');
                      
                         var_dump($xml);
                      } else {
                         exit('Failed to open test.xml.');
                      }
                      ?> 
                      <?php
                      if (file_exists('xml.php')) {
                         $xml = file_get_contents('xml.php');
                      
                         var_dump($xml);
                      } else {
                         exit('Failed to open test.xml.');
                      }
                      ?> 

                      And remember the difference between a filename and a URL.

                        Thanks so much buddy.
                        Now I got it.
                        Looking at this post what a mess :rolleyes:
                        but it was very useful.
                        Bye.

                          Write a Reply...