469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Issues with simplexml_load_file

33
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:

Expand|Select|Wrap|Line Numbers
  1. $xml = simplexml_load_file("news_headlines_xml.php");
This is generating a parser error:
Warning: simplexml_load_file() [function.simplexml-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.simplexml-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:

Expand|Select|Wrap|Line Numbers
  1. // Begin XML
  2. $xmlString =  '<?xml version="1.0" encoding="utf-8"?>';
  3. $xmlString .= "<NEWS>";
  4.  
  5. // Begin data output
  6. if($getPostCount<count($posts)){
  7.     for($b=0;$b<$getPostCount;$b++){
  8.         $xmlString .= "<ARTICLE>";
  9.         $xmlString .= "<AUTHOR>" . $postArray[$posts[$b]]['poster_name'] . "</AUTHOR>";
  10.         $xmlString .= "<SUBJECT>" . str_replace("\"", "&quot;", $postArray[$posts[$b]]['post_subject']) . "</SUBJECT>";
  11.         $xmlString .= "<TIME>" . $postArray[$posts[$b]]['post_time'] . "</TIME>";
  12.         $xmlString .= "</ARTICLE>";
  13.     }
  14. }
  15. $xmlString .= "</NEWS>";
  16.  
  17. echo $xmlString;
  18.  
I am pretty new to XML, is this even possible? If not, what other methods are available to me?
May 31 '09 #1
7 10546
Atli
5,058 Expert 4TB
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?
May 31 '09 #2
flydev
33
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 =(
May 31 '09 #3
Atli
5,058 Expert 4TB
Ok. Did that fix the simplexml issue to?

I tried this a simple test on my own server, and it worked fine.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("content-type: text/plain");
  3.  
  4. $ex = simplexml_load_file("http://www.airfrontierva.com/FedEx4/news_headlines_xml.php");
  5.  
  6. foreach($ex->ARTICLE as $_article) {
  7.     echo "Article: \n";
  8.     echo " - Author:\t", $_article->AUTHOR, "\n";
  9.     echo " - Subject:\t", $_article->SUBJECT, "\n";
  10.     echo " - Time:\t", $_article->TIME, "\n";
  11. }
  12. ?>
And I got these results:
Expand|Select|Wrap|Line Numbers
  1. Article: 
  2.  - Author:    Anonymous
  3.  - Subject:    Filing Flight Reports
  4.  - Time:    1213043343
  5. Article: 
  6.  - Author:    FDX250 Fred
  7.  - Subject:    Website Address in Remarks Section of Flightplan
  8.  - Time:    1220920102
  9. Article: 
  10.  - Author:    FDX1032 Jesse
  11.  - Subject:    Pilot Operating Handbook Update
  12.  - Time:    1222778802
  13. Article: 
  14.  - Author:    FDX631 - Renato
  15.  - Subject:    NOTAM - South America - SID CHANGE of HUB Campinas
  16.  - Time:    1224599515
  17. Article: 
  18.  - Author:    FDX631 - Renato
  19.  - Subject:    HUB Campinas (SBKP) VOR PSN/PIR
  20.  - Time:    1227142677
May 31 '09 #4
flydev
33
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.simplexml-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.simplexml-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.simplexml-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
May 31 '09 #5
Atli
5,058 Expert 4TB
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.
May 31 '09 #6
flydev
33
I see, i'll petition my provider to enable URL access....thanks so much for your help!
May 31 '09 #7
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3.  * File: createXML.php
  4.  */
  5.   $xmlString = "";
  6.   // Insert code to create some XML here
  7.  
  8.   return $xmlString
  9. ?>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3.  * File: index.php
  4.  */
  5. $xmlString = include("createXML.php");
  6.  
  7. $parsed = simplexml_load_string($xmlString);
  8.  
  9. // etc...
  10. ?>
May 31 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Rainer Mohr | last post: by
3 posts views Thread by eschneider | last post: by
2 posts views Thread by Harsha | last post: by
7 posts views Thread by alexus | last post: by
4 posts views Thread by newzdog | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.