关于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( [email protected]_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( [email protected]_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);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-01 11:51:18