'php echo xml documents with header
php code
<?php
$xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml.="<db_entry id=\"$model->id\" doi=\"$model->identifier\">";
$xml.="<dataset>";
$xml.="</dataset>";
$xml.="</db_entry>";
$xml=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $xml);
$output= simplexml_load_string($xml);
echo $output->asXML();
when i run it, it can't shows the xml format in the webpage(shows all text without xml tag). so i add the header(), but it shows XML Parsing Error: XML or text declaration not at start of entity.
<?php header("Content-type: text/xml"); ?>
<?php
$xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml.="<db_entry id=\"$model->id\" doi=\"$model->identifier\">";
$xml.="<dataset>";
$xml.="</dataset>";
$xml.="</db_entry>";
$xml=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $xml);
$output= simplexml_load_string($xml);
echo $output->asXML();
the webpage shows empty, but in the page view source it shows
<?xml version="1.0" encoding="UTF-8"?>
<db_entry id="5" doi="110039"><dataset/></db_entry>
and has three blank lines at the beginning.
Solution 1:[1]
Change this area
<?php header("Content-type: text/xml"); ?>
<?php
$xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
To
<?php
header("Content-type: text/xml");
$xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
REASON
By closing php in one line and opening again in the next line you are writing a line to the output. As you know, things outside <?php ?>
tag will directly print to the browser. And as per XML rule, you cannot put anything before the <?xml ...?>
part.
Solution 2:[2]
if you can try to print using this so that its work properly
echo '';print_r($output);echo '';die;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | T???? M?????? |
Solution 2 | cursorrux |