php 產生xml檔案

來源:互聯網
上載者:User

標籤:blog   http   io   ar   使用   for   sp   檔案   資料   

最近幫朋友最佳化一個網站,想產生xml格式的sitemap然後提交給搜尋引擎,利用php的simpleXML類就很容易實現了。貼一下代碼塊:

//sitemap_data.php 包含了網站所有連結的資訊,直接貼出輸出的資料,源碼就不貼了array(22) {  [0]=>  array(1) {    ["loc"]=>    string(32) "http://www.ibxg.com.cn/index.php"  }  [1]=>  array(1) {    ["loc"]=>    string(32) "http://www.ibxg.com.cn/about.php"  }  [2]=>  array(1) {    ["loc"]=>    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=1"  }  [3]=>  array(1) {    ["loc"]=>    string(55) "http://www.ibxg.com.cn/news_center.php?news_center_id=2"  }  [4]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=1"  }  [5]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=2"  }  [6]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=3"  }  [7]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=4"  }  [8]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=5"  }  [9]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=6"  }  [10]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=7"  }  [11]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=8"  }  [12]=>  array(1) {    ["loc"]=>    string(43) "http://www.ibxg.com.cn/product.php?cat_id=9"  }  [13]=>  array(1) {    ["loc"]=>    string(32) "http://www.ibxg.com.cn/order.php"  }  [14]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=1"  }  [15]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=2"  }  [16]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=3"  }  [17]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=4"  }  [18]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=5"  }  [19]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=6"  }  [20]=>  array(1) {    ["loc"]=>    string(47) "http://www.ibxg.com.cn/project.php?project_id=7"  }  [21]=>  array(1) {    ["loc"]=>    string(34) "http://www.ibxg.com.cn/contact.php"  }}


sitemap_xml.php檔案

<?phpheader(‘Content-Type: text/xml‘);//這行很重要,php預設輸出text/html格式的檔案,所//以這裡明確告訴瀏覽器輸出的格式為xml,不然瀏覽器顯示不出xml的格式require_once(‘sitemap_data.php‘); //把資料來源載入進來$sitemap=$sitemap; //這裡要按照sitemap的格式構造出xml的檔案,urlset url loc是規定必須有的標籤$xml_wrapper = <<<XML<?xml version=‘1.0‘ encoding=‘utf-8‘?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>XML;//$xml = simplexml_load_string($xml_wrapper);$xml = new SimpleXMLElement($xml_wrapper);foreach ($sitemap as $data) {    $item = $xml->addChild(‘url‘); //使用addChild添加節點    if (is_array($data)) {        foreach ($data as $key => $row) {            $node = $item->addChild($key, $row);            if (isset($attribute_array[$key]) && is_array($attribute_array[$key])) {                foreach ($attribute_array[$key] as $akey => $aval) {//設定屬性值,我這裡為空白                    $node->addAttribute($akey, $aval);                }            }        }    }}echo $xml->asXML(); //用asXML方法輸出xml,預設只構造不輸出。?>

另外網上也找到其他方法比如DOMDocument來構造xml,但通過比較使用simpleXML類是最省代碼,實現起來也很簡單。

其他方法可以參考這文章

http://www.phppan.com/2009/10/use-php-create-xml-file/

本文出自 “安家圈子交流學習” 部落格,請務必保留此出處http://brucetam.blog.51cto.com/1863614/1573444

php 產生xml檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.