donglu1913 2014-08-16 11:44
浏览 103
已采纳

PHP脚本自动将sitemap.xml转换为格式良好的可点击的sitemap.html? [关闭]

I have the sitemap of my website in an XML file in this format:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" 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" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.mywebsite.com//index.html</loc>
    <lastmod>2014-08-16</lastmod>
    <priority>0.5</priority>
  </url>
  <url>
// ... etc.

How can I automatically create an HTML page from this XML file containing a well formatted nice-looking clickable sitemap for the visitors of my website? Ideally this should be a PHP script which outputs the desired HTML to the browser.

EDIT: I have created some code which you can look at in the accepted solution. How can this code be optimized?

  • 写回答

1条回答 默认 最新

  • douchaqi3369 2014-08-16 17:45
    关注

    Since so far nobody provided a solution, I wrote my own solution:

    <?php
    
    $thisbasedir = '../mywebsite/';
    $dom = new DomDocument();
    $dom->load($thisbasedir . "sitemap.xml");
    $data = $dom->getElementsByTagName('loc');
    
    echo '<!DOCTYPE html>';
    echo '<HTML>';
    echo '<HEAD>';
    echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    echo '</HEAD>';
    echo '<BODY>';
    echo ("<table>");
    
    foreach ($data as $node)
    {
        echo '<tr>';
        $thisurl = $node->textContent;
        $thisbasename = basename($thisurl);
        $thistitle = get_title_tag_from_htmlpage($thisbasedir . $thisbasename);
        $thisdescription = get_description_from_htmlpage($thisbasedir . $thisbasename);
        echo ("<td>" .
              '<a href="' . $thisurl . '">' .
                  $thistitle . ' (' . $thisdescription . ')' .
              '</a>' .
              "</td>");
        echo '</tr>';
    }
    
    echo ("</table>");
    echo '</BODY>';
    echo '</HTML>';
    
    function get_description_from_htmlpage($ahtmlfile)
    {
        $tags = get_meta_tags($ahtmlfile);
        $thisdescription = $tags['description'];
        if (isset($thisdescription))
            return $thisdescription;
        else
            return 'No description';
    }
    
    function get_title_tag_from_htmlpage($ahtmlfile)
    {
        $thisfilecontents = file_get_contents($ahtmlfile);
        if (preg_match('/<title>(.+)<\/title>/', $thisfilecontents, $matches) && isset ($matches[1]))
            return $matches[1];
        else
            return "No Title";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面