我寫了一個php利用DOMDocument 組件產生rss檔案的代碼,但是感覺太過於臃腫,想封裝成一個類但是一直沒成功,特來請教
產生後rss大體是這樣的
1123木頭
1
http://www.xxx.com
id:1,user_name:,pass:123,real_name:木頭
2456小木頭
2
http://www.xxx.com
id:2,user_name:,pass:456,real_name:小木頭
當然代碼太過於冗餘
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
//建立標籤
//建立rss標籤
$rss = $doc->createElement('rss');
//建立channel下面的標籤
$channel = $doc->createElement('channel');
$ctitle = $doc->createElement('title');
$clink = $doc->createElement('link');
$cdescription = $doc->createElement('description');
foreach ($arr as $key => $val) {
//建立item標籤
$item = $doc->createElement('item');
//建立item下的子標籤標籤
$user = $doc->createElement('user');
$ititle = $doc->createElement('title');
$ilink = $doc->createElement('link');
$idescription = $doc->createElement('description');
//建立user標籤
$user_id = $doc->createElement('user_id');
$user_name = $doc->createElement('user_name');
$user_pass = $doc->createElement('user_pass');
$real_name = $doc->createElement('real_name');
/*這裡是需要資料庫迴圈調用的地方*/
//建立內容
//建立item下面的標籤的內容
$c_ititle = $doc->createTextNode($val['user_id']);
$c_ilink = $doc->createTextNode('http://www.xxx.com');
$c_idescription = $doc->createTextNode('id:'.$val['user_id'].',user_name:'.$val['user_name'].',pass:'.$val['pass'].',real_name:'.$val['real_name']);
//建立user下面的標籤的內容
$c_user_id = $doc->createTextNode($val['user_id']);
$c_user_name = $doc->createTextNode($val['user_name']);
$c_user_pass = $doc->createTextNode($val['pass']);
$c_real_name = $doc->createTextNode($val['real_name']);
//建立item屬性值
$a_id = $doc->createTextNode($val['user_id']);
//建立屬性
$attributes = $doc->createAttribute('id');
/*這裡是需要資料庫迴圈調用的地方*/
//item一級標籤的元素內容繼承
$ititle->appendChild($c_ititle);
$ilink->appendChild($c_ilink);
$idescription->appendChild($c_idescription);
//user一級標籤的元素內容繼承
$user_id->appendChild($c_user_id);
$user_name->appendChild($c_user_name);
$user_pass->appendChild($c_user_pass);
$real_name->appendChild($c_real_name);
/*這裡是需要資料庫迴圈調用的地方*/
//繼承
$channel->appendChild($item);
//item一級標籤的標籤繼承
$item->appendChild($user);
$item->appendChild($ititle);
$item->appendChild($ilink);
$item->appendChild($idescription);
//id=1
$attributes->appendChild($a_id);
//
$item->appendChild($attributes);
//item一級標籤的標籤繼承
$user->appendChild($user_id);
$user->appendChild($user_name);
$user->appendChild($user_pass);
$user->appendChild($real_name);
}
//建立內容
//建立channel下面的標籤的內容
$c_ctitle = $doc->createTextNode('測試rss');
$c_clink = $doc->createTextNode('http://www.xxx.com');
$c_cdescription = $doc->createTextNode('這是一個測試rss');
//建立rss版本屬性值
$rss_attribute_c = $doc->createTextNode('2.0');
//建立rss版本屬性
$rss_attribute = $doc->createAttribute('version');
//繼承
//channel一級標籤的元素內容繼承
$ctitle->appendChild($c_ctitle);
$clink->appendChild($c_clink);
$cdescription->appendChild($c_cdescription);
//version="2.0"
$rss_attribute->appendChild($rss_attribute_c);
//channel一級標籤的標籤繼承
$channel->appendChild($ctitle);
$channel->appendChild($clink);
$channel->appendChild($cdescription);
//建立根節點
$rss->appendChild($channel);
$rss->appendChild($rss_attribute);
$doc->appendChild($rss);
//儲存xml檔案
$doc->save('hello.xml');
function connect_sql(){
$con = mysql_connect('localhost','root','123456') or die('mysql error :'.mysql_error());
mysql_select_db('test');
mysql_query('set names utf8');
}
function getall(){
$sql = "SELECT * FROM test";
$query = mysql_query($sql);
$content = array();
while($row = mysql_fetch_array($query)){
$content[] = $row;
}
return $content;
}
請教各位大牛,怎樣改裝成一個class,我試了,一直不行
回複討論(解決方案)
為什麼要用 dom 呢?
直接字串拼裝不就可以的嗎?如
$ar = array( array( 'id' => 1, 'user' => array('user_id' => 1, 'user_name' => '', 'user_pass' => 123, 'real_name' => 'aa'), 'title' => 1, 'link' => 'http://www.xxx.com', 'description' => 'id:1,user_name:,pass:123,real_name:aa', ), array( 'id' => 2, 'user' => array('user_id' => 2, 'user_name' => '', 'user_pass' => 456, 'real_name' => 'bb'), 'title' => 2, 'link' => 'http://www.xxx.com', 'description' => 'id:1,user_name:,pass:456,real_name:bb', ),);
11http://www.xxx.comid:1,user_name:,pass:123,real_name:aa22http://www.xxx.comid:1,user_name:,pass:456,real_name:bb
為什麼要用 dom 呢?
直接字串拼裝不就可以的嗎?如
$ar = array( array( 'id' => 1, 'user' => array('user_id' => 1, 'user_name' => '', 'user_pass' => 123, 'real_name' => 'aa'), 'title' => 1, 'link' => 'http://www.xxx.com', 'description' => 'id:1,user_name:,pass:123,real_name:aa', ), array( 'id' => 2, 'user' => array('user_id' => 2, 'user_name' => '', 'user_pass' => 456, 'real_name' => 'bb'), 'title' => 2, 'link' => 'http://www.xxx.com', 'description' => 'id:1,user_name:,pass:456,real_name:bb', ),);
11http://www.xxx.comid:1,user_name:,pass:123,real_name:aa22http://www.xxx.comid:1,user_name:,pass:456,real_name:bb
直接echo輸出,然後fwrite寫到一個xml檔案中,這個好弄,只是我覺得既然php有內建的domdocument組件,那麼是不是肯定有他的優點相比於直接拼湊組建檔案,這個目前不清楚