2014-11-21 15 views
0

와의 sitemap.xml하는 것입니다변환 TXT 포함 I 텍스트 데이터베이스가있는 것으로 추측 PHP

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"> 

답변

0

당신은 http://php.net/manual/en/function.xmlwriter-start-attribute-ns.php 를 사용하거나 사용할 수 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(); 
0

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과 약간 다르며 필요에 따라 네임 스페이스 정의를 추가합니다.

$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 특성은 선택 사항이어야합니다.