-2

i am trying to generate xml document with the help of php but i am getting some issue. Please help Shows this error: This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error.

<?php
$con = mysqli_connect('localhost', 'ypnepalc_user','ypnepal@123', 'ypnepalc_main');

$listings = $con->query("SELECT url FROM listings");
$array_listings =[];
if($listings -> num_rows > 0) {
    while ($row = $listings -> fetch_assoc()) {
        $array_listings[] = $row['url'];
        
    }
}
$data_xml= 'urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9';
$static_url = [
    'https://ypnepal.com.np',
    
    ];
    foreach ($array_listings as $url) {
        $date = ('c');
        $pr = '0.80';
        if($url == 'https://ypnepal.com.np') $pr='1.00';
        $data_xml .="
            <url>
                <loc>$url</loc>
                <lastmod>$date</lastmod>
                <priority>$pr</priority>
            </url>
        ";
    }
$data_xml .= '</urlset>';
$file = fopen ('sitemap.xml', 'w');
fwrite($file, $data_xml);
  • 1
    `"Below is a rendering of the page up to the first error"` - I see no XML, only the PHP that attempts to generate it. Can you add the XML? Most likely is some special characters in the data but impossible to say – Professor Abronsius Aug 11 '23 at 07:15
  • 2
    It's usually safer to not build your own xml content and use the api's provided (simplexml or domdocument) – Nigel Ren Aug 11 '23 at 07:18
  • 2
    `$data_xml .= '';` - right, so that is the closing tag of your root element. `$data_xml= 'urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9';` - so where are the `<...>` around this then, to make it the corresponding opening _tag_? Right now it is just random text gibberish, that of course has no place being there in a well-formed XML document. – CBroe Aug 11 '23 at 07:25

0 Answers0