Issues with simplexml_load_file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flydev
    New Member
    • Feb 2008
    • 33

    Issues with simplexml_load_file

    Hi,

    I am having issues using simplexml. I have generated an XML file using PHP (link), and I am trying to load it into a simplexml object using:

    Code:
    $xml = simplexml_load_file("news_headlines_xml.php");
    This is generating a parser error:
    Warning: simplexml_load_ file() [function.simple xml-load-file]: news_headlines_ xml.php:72: parser error : Start tag expected, '<' not found in /home/airfront/public_html/FedEx4/functions.php on line 5

    Warning: simplexml_load_ file() [function.simple xml-load-file]: $xmlString = '<?xml version="1.0" encoding="utf-8"?>'; in /home/airfront/public_html/FedEx4/functions.php on line 5
    It looks as if it's trying to parse the actual PHP script, and not the generated XML. I have set the MIME type on the PHP file to text/xml. Here is the script that generates the XML:

    Code:
    // Begin XML
    $xmlString =  '<?xml version="1.0" encoding="utf-8"?>';
    $xmlString .= "<NEWS>";
    
    // Begin data output
    if($getPostCount<count($posts)){
    	for($b=0;$b<$getPostCount;$b++){
    		$xmlString .= "<ARTICLE>";
    		$xmlString .= "<AUTHOR>" . $postArray[$posts[$b]]['poster_name'] . "</AUTHOR>";
    		$xmlString .= "<SUBJECT>" . str_replace("\"", "&quot;", $postArray[$posts[$b]]['post_subject']) . "</SUBJECT>";
    		$xmlString .= "<TIME>" . $postArray[$posts[$b]]['post_time'] . "</TIME>";
    		$xmlString .= "</ARTICLE>";
    	}
    }
    $xmlString .= "</NEWS>";
    
    echo $xmlString;
    I am pretty new to XML, is this even possible? If not, what other methods are available to me?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    That should work fine, yea.

    However, the link you gave us isn't outputting anything.
    Therefore your simplexml_load_ file call fails, having nothing to parse.

    Are you sure the script that generates the XML is working correctly?
    Did you perhaps forget to upload it, or uploaded a old version of it?

    Comment

    • flydev
      New Member
      • Feb 2008
      • 33

      #3
      Doh, I apologize, I was using a work-around, which broke that file...it's back to the way it was, and it looks like a valid XML file =(

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok. Did that fix the simplexml issue to?

        I tried this a simple test on my own server, and it worked fine.
        [code=php]<?php
        header("content-type: text/plain");

        $ex = simplexml_load_ file("http://www.airfrontier va.com/FedEx4/news_headlines_ xml.php");

        foreach($ex->ARTICLE as $_article) {
        echo "Article: \n";
        echo " - Author:\t", $_article->AUTHOR, "\n";
        echo " - Subject:\t", $_article->SUBJECT, "\n";
        echo " - Time:\t", $_article->TIME, "\n";
        }
        ?>[/code]
        And I got these results:
        Code:
        Article: 
         - Author:	Anonymous
         - Subject:	Filing Flight Reports
         - Time:	1213043343
        Article: 
         - Author:	FDX250 Fred
         - Subject:	Website Address in Remarks Section of Flightplan
         - Time:	1220920102
        Article: 
         - Author:	FDX1032 Jesse
         - Subject:	Pilot Operating Handbook Update
         - Time:	1222778802
        Article: 
         - Author:	FDX631 - Renato
         - Subject:	NOTAM - South America - SID CHANGE of HUB Campinas
         - Time:	1224599515
        Article: 
         - Author:	FDX631 - Renato
         - Subject:	HUB Campinas (SBKP) VOR PSN/PIR
         - Time:	1227142677

        Comment

        • flydev
          New Member
          • Feb 2008
          • 33

          #5
          Interesting.... I copied your script to test.php, it errors out the same. I did have to change the path to the file because my server does not allow URL access. Perhaps this is a server issue, or if it's not give in URL format it interprets the file differently? (link)

          Warning: simplexml_load_ file() [function.simple xml-load-file]: news_headlines_ xml.php:73: parser error : Start tag expected, '<' not found in /home/airfront/public_html/FedEx4/test.php on line 4

          Warning: simplexml_load_ file() [function.simple xml-load-file]: $xmlString = '<?xml version="1.0" encoding="utf-8"?>'; in /home/airfront/public_html/FedEx4/test.php on line 4

          Warning: simplexml_load_ file() [function.simple xml-load-file]: ^ in /home/airfront/public_html/FedEx4/test.php on line 4

          Warning: Invalid argument supplied for foreach() in /home/airfront/public_html/FedEx4/test.php on line 6

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            If you give it a file path, it will read the file as it is... meaning it will read the PHP code.

            If you give it a URL, it will request the file via a HTTP server, which will execute the PHP code and return the output.

            Comment

            • flydev
              New Member
              • Feb 2008
              • 33

              #7
              I see, i'll petition my provider to enable URL access....thank s so much for your help!

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                No problem.

                But there is an easier solution.
                You can include your PHP file into your script and get the value like that.

                For example:
                [code=php]
                <?php
                /**
                * File: createXML.php
                */
                $xmlString = "";
                // Insert code to create some XML here

                return $xmlString
                ?>[/code]
                [code=php]
                <?php
                /**
                * File: index.php
                */
                $xmlString = include("create XML.php");

                $parsed = simplexml_load_ string($xmlStri ng);

                // etc...
                ?>[/code]
                Last edited by Atli; May 31 '09, 03:39 PM. Reason: Mixed up the <?php tags :P

                Comment

                Working...