Sign in to follow this  
Evil Booger

[web] Error on 1st line of XML file (I copied and pasted from an example)

Recommended Posts

I'm tryig to learn xml and I'm recieving an "unexpected T_STRING" on the first line of the external xml file. Here's my index.php:
<html>
<?php require('blogtastic.xml');?>
<body>
</body>
</html>


And here's blogtastic.xml, the file that creates the error(first line remember?):
<?xml version="1.0"?>
<moviebase>
 <movie>
  <title>
   PHP: Behind the Parser
  </title>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
 </movie>
</moviebase>


And here is the exact error:
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\php_practice\blogtastic.xml on line 1


I am using XAMPP(v 2.5) if that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
I'm tryig to learn xml and I'm recieving an "unexpected T_STRING" on the first line of the external xml file.



My assumption is that you have short_open_tag on.

With 'require', you are telling PHP to read the xml file, and interpret it. The <? that starts your xml header is interpreted as 'PHP starts here', so php happily continues parsing and will assume that 'xml version="1.0"' is PHP code.

There are two solutions, either turn off the short_open_tag configuration option, or change your xml file to read <?php echo '<?xml version="1.0">'; ?>

Share this post


Link to post
Share on other sites
But I thought that it didn't count since the file extension was .xml.

I choose the first option because everyone says to not use the short tags anyways. So...how do I do it? That's not the same as the http document is it?

Share this post


Link to post
Share on other sites
For the first solution, search for "short_open_tag" in your php.ini configuration file.

There's also a third consideration: simply delete the "<?xml ..." line from the xml file. This will have the effect of making the xml file not a valid xml document anymore - however, the resulting html document being served to the browser won't be valid html after including your xml document either way anyway.

PHP doesn't care about the file extensions of required/included files - the file will be processed the same no matter what extension the file is given.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
Ok, I found a way to do it using the require function. Its the first example on the php simple xml manual.


Don't do that. If a file is really XML, don't put PHP in it.

Use something like this (help.xml is the same content as on linked site):

<?

$raw = file_get_contents("help.xml");
$xml = new SimpleXMLElement($raw);


echo $xml->movie[0]->plot;

?>





This is better because:

0) the file remains valid xml
1) you don't have to depend on a magic variable name in the file

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this