How to include XML file in PHP

Discussion in 'PHP' started by KingCobra, Aug 31, 2013.

  1. #1
    Dear friends,

    I would like to include a xml file in my php file.
    But the php file giving me the following error:
    Parse error: syntax error, unexpected T_STRING in E:\WEB\Server\xampp\htdocs\test3\caching\news.xml on line 1

    I may identified the problem; as the code of xml file starting with "<?xml .....", its creates problem.

    Please tell me how can I include xml file.

    XML file: news.xml
    <?xml version='1.0' encoding='UTF-8' ?>
    <rss version='2.0'>
      <channel>
        <title>Daily News</title>
        <description>Description here</description>
        <link>http://www.rtvonline.com</link>
        <image>
                <title>Daily News</title>
                <link>http://www.mysite.com</link>
                <url>http://www.mysite.com/logo.png</url>
                <description>Description here</description>
        </image>
        <atom10:link xmlns:atom10='http://www.w3.org/2005/Atom' rel='self' type='application/rss+xml' />
    
        <feedburner:info xmlns:feedburner='http://rssnamespace.org/feedburner/ext/1.0' uri='rss/edition' />
        <atom10:link xmlns:atom10='http://www.w3.org/2005/Atom' rel='hub' href='http://pubsubhubbub.appspot.com/' />
    
        <item>
          <id>39343</id>
          <pubDate>2013-08-10</pubDate>
          <title><![CDATA[Eid Celebration ]]></title>
          <newsImage>http://www.mysite.com/10_08_13_01.jpg</newsImage>
          <newsVideo>http://www.mysite.com/10_08_13_01.mp4</newsVideo>
          <description><![CDATA[Text details are not available]]></description>
        </item>
        <item>
          <id>39348</id>
          <pubDate>2013-08-10</pubDate>
          <title><![CDATA[Report on Suicide ]]></title>
          <newsImage>http://www.mysite.com/10_08_13_06.jpg</newsImage>
          <newsVideo>http://www.mysite.com/10_08_13_06.mp4</newsVideo>
          <description><![CDATA[Text details are not available]]></description>
        </item>
        <item>
          <id>39349</id>
          <pubDate>2013-08-10</pubDate>
          <title><![CDATA[International News]]></title>
          <newsImage>http://www.mysite.com/10_08_13_07.jpg</newsImage>
          <newsVideo>http://www.mysite.com/10_08_13_07.mp4</newsVideo>
          <description><![CDATA[Text details are not available]]></description>
        </item>
        <item>
          <id>39350</id>
          <pubDate>2013-08-10</pubDate>
          <title><![CDATA[Sports News]]></title>
          <newsImage>http://www.mysite.com/10_08_13_08.jpg</newsImage>
          <newsVideo>http://www.mysite.com/10_08_13_08.mp4</newsVideo>
          <description><![CDATA[Text details are not available]]></description>
        </item>
      </channel>
    </rss>
    Code (markup):
    PHP file:
    <?php
    $cachefile = 'news.xml';
    include($cachefile);
    ?>
    PHP:
     
    KingCobra, Aug 31, 2013 IP
  2. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #2
    Well you should be able to do it with the include
    the shorter version would be
    
    <?php include 'news.xml'; ?>
    
    Code (markup):
    If its not working that means there are errors in your xml file

    How I solve xml problems if I cant see it visually is to limit the items to just one
    Then introduce each line and see what is causing the errors or not.

    But work with xml and php never cdata info, so I don't know if that is causing you errors.
     
    EmmanuelFlossie, Aug 31, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    You likely have short tags enabled, It's seeing the <? and treating it as the opening for PHP... so the XML doctype is being treated as PHP code.
    http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

    You either need to set "short_open_tag" to 0 in your php.ini -- but beware any scripts you use that rely upon it will therein break. (generally speaking shorttags are lazy bs anyways though)... or set it with ini_set:
    http://php.net/manual/en/function.ini-set.php

    
    ini_set('short_open_tag',0);
    
    Code (markup):
    Which you can set back to 1 after your include.

    Though if you aren't doing any processing or running any actual PHP inside your XML, you shouldn't be using include... that's readfile's job.
    http://us3.php.net/manual/en/function.readfile.php


    Which will dump the file to the output WITHOUT trying to run it as code.
    <?php
    readfile('news.xml');
    ?>
    Code (markup):
    Also, avoid declaring variables when you don't have to (like $cachefile) :D
     
    deathshadow, Aug 31, 2013 IP
  4. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Dear, @deathshadow & @WebStumblr, thank you for your replay.
    If I remove the very first line from xml file the php include command works fine. So problem is that xml tag starting with "<?" that is same as php starting. its the problem. This is why php includes may gives error.

    deathshadow,
    readfile('news.xml'); works fine. It can read the full xml file and no need to delete first line from xml file.
    But can you please tell me, which is better, which is safer and which is faster between include or readfile?
     
    KingCobra, Aug 31, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    To see which is faster and more secure, you have to understand what they do. "readfile" simply reads in the file and spits it out as output. "include" checks to see if there's any PHP in the file, and if so, run it. Because readfile doesn't attempt to run code, server-side it is by definition more secure (code read by it is NEVER run on the server), and since it doesn't have to run the file through the PHP parser, it's most certainly faster.

    You just can't do <?php ?> inside anything you readfile, as that will be echo'd out literally.

    I often find it easiest to describe by doing a simple demo:

    test.php
    <?php
    echo '
      <h1><code>readfile</code> vs. <code>include</code></h1>
      <hr />
      <h2><code>readfile</code></h2>
      <pre>',readfile('test.inc.php'),</pre>
      <hr />
      <h2><code>include</code></h2>
      <pre>',include('test.inc.php'),'</pre>
      <hr />
      <p>
        The first test should show simply the word "test" in the preformatted box, viewing the source will show that the PHP code was sent to the browser instead of being processed. The second test will display "this is a test" as the PHP code is run.
      </p>';
    ?>
    Code (markup):
    test.inc.php
    <?php echo 'this is a '; ?>test
    Code (markup):
    The sample output from running that would have this as it's source:
    
      <h1><code>readfile</code> vs. <code>include</code></h1>
      <hr />
      <h2><code>readfile</code></h2>
      <pre><?php echo 'this is a '; ?>test</pre>
      <hr />
      <h2><code>include</code></h2>
      <pre>this is a test</pre>
      <hr />
      <p>
        The first test should show simply the word "test" in the preformatted box, viewing the source will show that the PHP code was sent to the browser instead of being processed. The second test will display "this is a test" as the PHP code is run.
      </p>
    Code (markup):
    Because it does less and won't run code, readfile is more secure and faster -- though less versatile since it provides no means of processing or changing values in that XML file.
     
    deathshadow, Sep 1, 2013 IP
  6. sabato

    sabato Member

    Messages:
    407
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    43
    #6
    You can also generate the xml file from php so you will not have to include it.
     
    sabato, Sep 3, 2013 IP