php生成百度新闻源xml
<?php
/*
http://baike.baidu.com/view/125547.htm#2
百度网新闻开放协议
*/
mysql_connect($CFG['db_host'] ,$CFG['db_user'],$CFG['db_pass']) or die('数据库连接错误!');
mysql_select_db($CFG['db_name']);
mysql_query("set names utf8");
$p=$CFG['tb_pre'];
$ids=array(1,2,3);
foreach ($ids as $k=>$v){//begin foreach
$query="select `a`.*,`c`.*,`d`.*,`a`.`linkurl` as `alinkurl` from `{$p}category` as `c` ,`{$p}article_21` as `a` , `{$p}article_data_21` as `d` where `a`.`catid`=`c`.`catid` and `a`.`itemid`=`d`.`itemid` and `a`.`status`=3 and `a`.`catid`='{$v}' order by `a`.`itemid` desc limit 100";
$result=mysql_query($query);
$m=0;
while($info=mysql_fetch_array($result,MYSQL_ASSOC)){
$data_array[$m]['title']='';
$data_array[$m]['link']='';
$data_array[$m]['description']='';
$data_array[$m]['text']='';
$data_array[$m]['image']='';
$data_array[$m]['keywords']='';
$data_array[$m]['category']='';
$data_array[$m]['author']='';
$data_array[$m]['source']='';
$data_array[$m]['pubDate']=''
$m++;
}
/*$data_array = array(
array(
"title"=>"",
"link"=>"",
"description"=>"",
"text"=>"",
"image"=>"123",
"keywords"=>'',
"category"=>"",
"author"=>"",
"source"=>"",
"pubDate"=>"2012-06-12 14:03"
)
);*/
// 属性数组
/*$attribute_array = array(
'title' => array(
'size' => 1
)
);
*/
// 创建一个XML文档并设置XML版本和编码。。
$dom=new DomDocument('1.0', 'gbk');
// 创建根节点document
$document = $dom->createElement('document');
$dom->appendchild($document);
//创建document->website
$website=$dom->createElement('website');
$document->appendchild($website);
$mytext = $dom->createTextNode('http://www.abc.net/');
$website->appendchild($mytext);
//创建document->webMaster
$webmaster=$dom->createElement('webMaster');
$document->appendchild($webmaster);
$mytext = $dom->createTextNode('songzhenghe89@126.com');
$webmaster->appendchild($mytext);
//创建document->updatePeri
$updateperi=$dom->createElement('updatePeri');
$document->appendchild($updateperi);
$mytext = $dom->createTextNode('60');
$updateperi->appendchild($mytext);
//批量创建document->item
foreach ($data_array as $data) {
$item = $dom->createElement('item');//创建一个item
$document->appendchild($item);
create_item($dom, $item, $data, $attribute_array);
}
//echo $dom->saveXML();
$dom->save("./"."{$v}.xml");
}//end foreach
function create_item($dom, $item, $data, $attribute) {
if (is_array($data)) {
foreach ($data as $key => $val) {
// 创建元素
$$key = $dom->createElement($key);
$item->appendchild($$key);
// 创建元素值
if($key!="pubDate"){
//使用cdata包含
$cdatasection=$dom->createCDATASection($val);
$$key->appendchild($cdatasection);
}else{
$mytext = $dom->createTextNode($val);
$$key->appendchild($mytext);
}
if (isset($attribute[$key])) {
// 如果此字段存在相关属性需要设置
foreach ($attribute[$key] as $akey => $row) {
// 创建属性节点
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// 创建属性值节点
$aval = $dom->createTextNode($row);
$$akey->appendChild($aval);
}
} // end if
}
} // end if
} // end function
?>