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";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂