Supposed that I have text database include is:
http://example.com,monthly,0.3
http://example.com/one,daily,0.5
http://example.com/two,weekly,0,8
I would like to convert to my text database to sitemap.xml.
$fp = fopen('./database.txt', 'r');
$xml = new XMLWriter;
$xml->openURI('./sitemap.xml');
$xml->setIndent(true);
$xml->startElement('urlset');
while ($line = fgetcsv($fp)) {
if (count($line) < 4) continue;
$xml->startElement('url');
$xml->writeElement('loc', $line[0]);
$xml->writeElement('changefreq', $line[1]);
$xml->writeElement('priority', $line[2]);
echo $xliff->getDocument();
$xml->endElement();
}
$xml->endElement();
I tried this code, I couldn't add this part...
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
The
xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
is a namespace definition. It defines that all element nodes (without a prefix are part of this format. You can read the element tag names as{http://www.sitemaps.org/schemas/sitemap/0.9}:urlset
,{http://www.sitemaps.org/schemas/sitemap/0.9}:url
, ...It allows to mix XML formats without conflicts.
To create an XML node with XML reader that need a namespace definition you will have to use the *NS variants of the methods. This is a little different from DOM, where you always provide the namespace and it adds namespace definitions as needed.
Output:
NOTE: The
xsi:schemaLocation
attribute should be optional.