码迷,mamicode.com
首页 > Web开发 > 详细

php中的关于xml的使用(SEO)

时间:2015-08-04 23:08:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:php   xml   

关于xml使用,这个地方是为了SEO收录写的两个函数,仅供参考,欢迎交流,有问题欢迎提问,具体的函数可以查文档。

function edit_xml_file($xml_file_path,$datas,$is_index = false,$is_compress = true){
     $doc = new DomDocument(‘1.0‘, ‘utf-8‘);
     $doc->formatOutput = true;
     $flag = false;
     if( !@file_exists($xml_file_path) || !$doc->load($xml_file_path)) {    
        $flag = create_xml_file($xml_file_path,$datas,$is_index,$is_compress);
         return $flag;
     } 
    //读取文件数据
    $xmldata = simplexml_load_file($xml_file_path);
    $newxmldata = array();
    $xmL_url = array();
    foreach ($xmldata as $key=>$obj){
         $obj = (array) $obj;
         if(isset($obj[‘lastmod‘])){
             $obj[‘lastmod‘] = date(‘c‘,time());
         }
         $xmL_url[] = $obj[‘loc‘];
         $newxmldata[][$key] = $obj;
    }
    //将新添加的数据放在一起
    foreach ($datas as $data){
        if(!in_array($data[$key][‘loc‘], $xmL_url)){
            $newxmldata[] = $data;
        }
    }

    //根据新数据重新更新创建文件
    if(!empty($newxmldata) && unlink($xml_file_path)){
        $flag = create_xml_file($xml_file_path,$newxmldata,$is_index,$is_compress);
    }
    unset($datas);
    return $flag;
}

/** xml,追加数据
 * @param xml_file_path 文件路径,$data 数据,$is_index 为了区分是地图文件还是地图索引,默认是地图文件
 * @author zdj
 * @date 2015-01-23
 */
function add_data_xml_file($xml_file_path,$datas,$is_index = false,$is_compress = true){
    $doc = new DomDocument(‘1.0‘, ‘utf-8‘);
    $doc->formatOutput = true;
    $flag = false;
    if( !@file_exists($xml_file_path) || !$doc->load($xml_file_path)) {
        $flag = create_xml_file($xml_file_path,$datas,$is_index,$is_compress);
        return $flag;
    }
    $root = $doc->documentElement;
    foreach ($datas as $data){
        foreach ($data as $key=>$obj){
            $key_dom = $doc->createElement($key);
            foreach ($obj as $attribute =>$attribute_value){
                $attribute_dom = $doc->createElement($attribute);
                $attribute_dom_value = $doc->createTextNode($attribute_value);
                $attribute_dom->appendChild($attribute_dom_value);
                $key_dom->appendChild($attribute_dom);
            }
            $root->appendChild($key_dom);
        }
    }
    $doc->appendChild($root);
    $doc->save($xml_file_path);
    //创建压缩的文件
    $xml_gz_file_path = $xml_file_path.‘.gz‘;
    if($is_compress){
        $fp = gzopen ($xml_gz_file_path, ‘w9‘);
        gzwrite ($fp, $doc->saveXML());
        gzclose($fp);
    }
    //给文件赋予权限//给文件赋予权限
    if(file_exists($xml_file_path)){
        chmod($xml_file_path, 0744);
    }
    if(file_exists($xml_gz_file_path)){
        chmod($xml_gz_file_path, 0744);
    }
    unset($datas);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

php中的关于xml的使用(SEO)

标签:php   xml   

原文地址:http://blog.csdn.net/biboqingtian/article/details/47282279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!