Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将txt转换为sitemap.xml_Php_Xml_Text_Sitemap_Sitemap.xml - Fatal编程技术网

使用PHP将txt转换为sitemap.xml

使用PHP将txt转换为sitemap.xml,php,xml,text,sitemap,sitemap.xml,Php,Xml,Text,Sitemap,Sitemap.xml,假设我有文本数据库,包括: http://example.com,monthly,0.3 http://example.com/one,daily,0.5 http://example.com/two,weekly,0,8 我想将我的文本数据库转换为sitemap.xml $fp = fopen('./database.txt', 'r'); $xml = new XMLWriter; $xml->openURI('./sitemap.xml'); $xml->setIndent(

假设我有文本数据库,包括:

http://example.com,monthly,0.3
http://example.com/one,daily,0.5
http://example.com/two,weekly,0,8
我想将我的文本数据库转换为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();
我尝试了此代码,但无法添加此部分

<?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">
你可以用 或使用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();
xmlns模型http://www.sitemaps.org/schemas/sitemap/0.9'是命名空间定义。它定义所有没有前缀的元素节点都是此格式的一部分。您可以按如下方式读取元素标记名称:{http://www.sitemaps.org/schemas/sitemap/0.9}:urlset{http://www.sitemaps.org/schemas/sitemap/0.9}:url

它允许混合XML格式而不会产生冲突

要使用需要名称空间定义的XML读取器创建XML节点,必须使用这些方法的*NS变体。这与DOM有点不同,DOM总是提供名称空间,并根据需要添加名称空间定义

$csv = <<<'CSV'
http://example.com,monthly,0,3
http://example.com/one,daily,0,5
http://example.com/two,weekly,0,8
CSV;
$lines = array_map('str_getcsv', explode("\n", $csv));

$xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$xml = new XMLWriter;
$xml->openMemory();
$xml->setIndent(true); 
$xml->startElementNS(NULL, 'urlset', $xmlns);
$xml->writeAttributeNS(
  'xsi', 
  'schemaLocation', 
  'http://www.w3.org/2001/XMLSchema-instance',
  'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
);

foreach ($lines as $line) {
   if (count($line) < 4) continue;
   $xml->startElement('url');
   $xml->writeElement('loc', $line[0]);
   $xml->writeElement('changefreq', $line[1]);
   $xml->writeElement('priority', $line[2]);
   $xml->endElement();
}
$xml->endElement();

echo $xml->outputMemory();
输出:

<urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 <url>
  <loc>http://example.com</loc>
  <changefreq>monthly</changefreq>
  <priority>0</priority>
 </url>
 <url>
  <loc>http://example.com/one</loc>
  <changefreq>daily</changefreq>
  <priority>0</priority>
 </url>
 <url>
  <loc>http://example.com/two</loc>
  <changefreq>weekly</changefreq>
  <priority>0</priority>
 </url>
</urlset>
注意:xsi:schemaLocation属性应该是可选的