Error in site map xml

Asked

Viewed 75 times

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.

1 answer

1


XML has four extra lines:

inserir a descrição da imagem aqui

Remove them and the problem is solved, the <?xml ... must be the first thing, there can be no kind of space before it.

  • I know I have to remove but can’t remove I can’t find that space in php

  • 3

    @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.

  • OK thanks all right I don’t know how I put when the code is too long

  • $root = $objDom->createelement("urlset");

  • 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.

  • http://pastebin.com/v0uqQPun Here is the whole code of include but I don’t think the problem comes from this

  • How stupid of me in the include file after php closed I had 4 lines :( Sorry

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.