dscqrkvr9562034621 2014-11-21 13:25
浏览 154

使用PHP将txt转换为sitemap.xml

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">
  • 写回答

2条回答 默认 最新

  • dowe98261 2014-11-21 13:59
    关注

    You could use http://php.net/manual/en/function.xmlwriter-start-attribute-ns.php or use DOM:

    $fp = fopen('./database.txt', 'r');
    $dom = new DOMDocument();
    $dom->formatOutput = true;
    $urlset = $dom->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
    $dom->appendChild($urlset);
    while ($line = fgetcsv($fp)) {
        var_dump($line);
        $url = $dom->createElement('url');
        $urlset->appendChild($url);
        $url->appendChild($dom->createElement('loc', $line[0]));
        $url->appendChild($dom->createElement('changefreq', $line[1]));
        $url->appendChild($dom->createElement('priority', $line[2]));
    }
    echo $dom->saveXML();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序