Error in site xml map

0

My site-map is giving the following error

XML Parsing Error: XML or text declaration not at start of entity
Location: http://amadeuantunes.com/site-map.php
Line Number 4, Column 1:<?xml version="1.0" encoding="UTF-8"?>
^

I noticed that it has a space and does not start at the 'zero' line

Here is the code that generates the sitemap:

<?php//firstline
header('Content-type: text/xml');
include 'pages/imagens.php';
$array = array(
    array(
        'loc' => 'http://amadeuantunes.com',
        'lastmod' => date('Y-m-d'),
        'changefreq' => 'weekly',
        'priority' => 1
    ),
);
    for($i =1; $i <= $imgSize; $i++)
    {

        array_push($array, array('loc' => 'http://amadeuantunes.com/img/portfolio/fullsize/'.$i .'.jpg','lastmod' => date('Y-m-d'),
        'changefreq' => 'never',
        'priority' => 0.5 ));


    }
$objDom = new DOMDocument('1.0');
$objDom->encoding = 'UTF-8';
$objDom->formatOutput = true;
$objDom->preserveWhiteSpace = true;
$root_attr = $objDom->createAttribute("xmlns");
$root->appendChild($root_attr);
$root = $objDom->createElement("urlset");
$objDom->appendChild($root);
$root_attr_text = $objDom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$root_attr->appendChild($root_attr_text);

if (!empty($array)) {

    foreach($array as $row) {

        $url = $objDom->createElement("url");
        $root->appendChild($url);

        $loc = $objDom->createElement("loc");
        $lastmod = $objDom->createElement("lastmod");
        $changefreq = $objDom->createElement("changefreq");
        $priority = $objDom->createElement("priority");

        $url->appendChild($loc);
        $url_text = $objDom->createTextNode($row["loc"]);
        $loc->appendChild($url_text);

        $url->appendChild($lastmod);
        $lastmod_text = $objDom->createTextNode($row["lastmod"]);
        $lastmod->appendChild($lastmod_text);

        $url->appendChild($changefreq);
        $changefreq_text = $objDom->createTextNode($row["changefreq"]);
        $changefreq->appendChild($changefreq_text);

        $url->appendChild($priority);
        $priority_text = $objDom->createTextNode($row["priority"]);
        $priority->appendChild($priority_text);

    }
}
echo $objDom->saveXML();
?>

Note, I noticed that when I put inlude this creates blanks but I can not remove those spaces.

    
asked by anonymous 02.03.2017 / 21:32

1 answer

1

XML has four extra lines:

Remove them and the problem will be solved, <?xml ... should be the first thing, there can be no space before it.

    
02.03.2017 / 21:38