我有一个站点地图与许多网址。比如:
<url>
<loc>
http://site.com/
</loc>
<priority>
0.50
</priority>
<changefreq>
daily
</changefreq>
<lastmod>
2011-07-27T06:58:53+00:00
</lastmod>
</url>
<url>
<loc>
http://site.com/link
等等……
我需要得到所有的链接在站点地图,没有别的。
我试过:
$links = file('sitemap.xml', FILE_IGNORE_NEW_LINES);
foreach($links as $link) {
echo $link;
}
现在它返回了所有的链接并保留了所有的<loc>, <priority>
等等但是它仍然包含了frequency, lastmod
等等等等....
输出如下所示:
http://site.com/ 11 0.50 12 daily 13 2011-07-27T06:58:53+00:00 14 15 16 http://site.com/page.html 17 0.40 18 daily 19 2011-07-
等....
我只需要得到链接,并把它放入一个数组。什么好主意吗?
谢谢。编辑:
下面是我使用的代码:$urls = array();
$xml='sitemap.xml';
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->loadXML("$xml"); // $DOMDocument->load('filename.xml');
$DomNodeList = $DomDocument->getElementsByTagName('from');
foreach($DomNodeList as $url) {
$urls[] = $url->nodeValue;
}
//display it
echo "<pre>";
print_r($urls);
echo "</pre>";
返回错误:Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 1
所以我试着测试它是否可以甚至加载xml:我把xml文件名改为无效的($xml='sit___emap.xml';
)
我应该得到一个错误,说它无法打开文件,但它却出现了与以前相同的错误,具有正确的文件名设置。所以我不认为这是sitemap
我无法让@AndreyKnupp的例子工作。以下是对我有用的:
$urls = array();
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load('filename.xml');
$DomNodeList = $DomDocument->getElementsByTagName('loc');
foreach($DomNodeList as $url) {
$urls[] = $url->nodeValue;
}
//display it
echo "<pre>";
print_r($urls);
echo "</pre>";
我检查了使用Levi Morrison (DOMDocument)方法vs taoufiqaitali方法(SimpleXML)的速度执行时间。结果是如此惊人,我必须与你分享。我的sitemap.xml里面有11140个链接(我的webgallery的sitemap)。
方法1 - DOMDocument -> 50.7秒执行时间
$start = microtime(true);
$urls = array();
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load('sitemap.xml');
$DomNodeList = $DomDocument->getElementsByTagName('loc');
foreach($DomNodeList as $url) {
$urls[] = $url->nodeValue;
}
echo "<pre>";
print_r($urls);
echo "</pre>";
$time_elapsed_secs = microtime(true) - $start;
echo $time_elapsed_secs . " seconds of execution time";
方法2 - SimpleXML -> 0.129秒执行时间
$start = microtime(true);
$urls = array();
$strXml = @file_get_contents('sitemap.xml');
$sitemap = @new SimpleXmlElement($strXml);
foreach($sitemap->url as $url) {
$urls[] = strval($url->loc);
}
echo "<pre>";
print_r($urls);
echo "</pre>";
$time_elapsed_secs = microtime(true) - $start;
echo $time_elapsed_secs . " seconds of execution time";
这是一个巨大的差异。SimpleXML方法几乎快了400倍。
你可以这样做…
<?php
$urls = array();
$DOMDocument = new DOMDocument();
$DOMDocument->preserveWhiteSpace = false;
$DOMDocument->loadXML($xml); // $DOMDocument->load('filename.xml');
$XPath = new DOMXPath($DOMDocument); // you can use getElementsByTagName
foreach($XPath->query('//url/loc') as $url) {
// $urls[$url->nodeName] = $url->nodeValue;
$urls[] = $url->nodeValue;
}
print_r($urls);
输出如下:
Array
(
[0] => http://site.com/
)
最简单的方法是
$strXml = @file_get_contents($url);
if (false == $strXml)
die('Could not open url. Check your spelling and try again');
$txt ="";
// So simple using SimpleXml
$sitemap = @new SimpleXmlElement($strXml);
foreach($sitemap->url as $url) {
$txt .= $url->loc . "'n";
}
使用任何 XML解析器?DOMDocument
, SimpleXML
, xml_parse
也可以使用simplexml
$xml=simplexml_load_file($file);
$links=$xml->xpath('//url/loc');
print_r($links);
编辑:可能需要使用strval
当你使用这些数组元素,因为它仍然被认为是一个SimpleXML对象。