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

Started by
6 comments, last by rip-off 16 years, 3 months ago
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.
Advertisement
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">'; ?>
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?
For some reason, the text between the <plot> tags are printed onto the screen. The text in the title tags are put as the page's title. I think my comp's trating it like a html page.
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.
What are you trying to do?

If you don't want PHP to interpret the file as a potential source of PHP code, use a different function, not require(). I have relatively little experience in PHP, but these two (file, readfile) might be more suited to whatever you are trying.
Ok, I found a way to do it using the require function. Its the first example on the php simple xml manual.
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

This topic is closed to new replies.

Advertisement