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 line 'zero'
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’ve noticed that when you put inlude it creates blank spaces but I can’t remove those spaces.
I know I have to remove but can’t remove I can’t find that space in php
– Amadeu Antunes
@Amadeuantunes instead of posting the code in the direct post Pastebin in the question and use the correct tags, you only used the XML tag I assumed that the problem had nothing to do with PHP.... You already have a lot of time here on the site and I’m sure you know how things work. Please make it easy for those who will answer. Understand as a constructive criticism.
– Guilherme Nascimento
OK thanks all right I don’t know how I put when the code is too long
– Amadeu Antunes
$root = $objDom->createelement("urlset");
– Amadeu Antunes
What’s in your include? I changed the order of
$root = $objDom->createElement("urlset");
and$root->appendChild($root_attr);
, so it worked, you probably dragged the line and so the code got wrong... but still didn’t generate extra spaces and lines, the problem is for sure in your include.– Guilherme Nascimento
http://pastebin.com/v0uqQPun Here is the whole code of include but I don’t think the problem comes from this
– Amadeu Antunes
Let’s go continue this discussion in chat.
– Guilherme Nascimento
How stupid of me in the include file after php closed I had 4 lines :( Sorry
– Amadeu Antunes