iT邦幫忙

DAY 26
10

XML系列 第 26

[XML]26-建置之Server端

介紹過了使用實例及 XML 規格之後,接著就是進入實作的部份,此篇先從 Server 端開始。
要建置一個 XML server 非常簡單,只要你會由資料庫取得資料並且轉成 HTML 網頁顯示在網站上,那基本上你已經具備提供 XML 資料所須的技術能力了。

以iT邦幫忙的 RSS 為例如下:

<rss version="2.0" encoding="UTF-8">
<channel>
<title>iT邦幫忙</title>
<description></description>
<link>http://ithelp.ithome.com.tw/</link>
<lastBuildDate>Thu, 13 Oct 2011 15:07:56 GMT</lastBuildDate>
<generator>FeedCreator 1.7.2-ppt (info@mypapit.net)</generator>
<item>
<title>資訊學院的30門課-計算機組織與結構</title>
<link>http://ithelp.ithome.com.tw/question/10076053?tag=rss.qu</link>
<description>這門課我覺得是大學最有趣的一門課,直接與電腦硬體有關係,80386SX、80386DX、80486、Pentium等等的演進,也在這門課有的功能性的瞭解。</description>
<author>kradark</author>
<pubDate>Thu, 13 Oct 2011 05:23:45 GMT</pubDate>
</item>
<item>
<title>Opening</title>
<link>http://ithelp.ithome.com.tw/question/10076047?tag=rss.qu</link>
<description>比賽開始~</description>
<author>jackychu</author>
<pubDate>Thu, 13 Oct 2011 05:18:26 GMT</pubDate>
</item>
<item>
<title>[閱讀雜感] Ubuntu Linux 30 天 01: 30 Days With Ubuntu Linux</title>
<link>http://ithelp.ithome.com.tw/question/10076046?tag=rss.qu</link>
<description></description>
<author>powerop</author>
<pubDate>Thu, 13 Oct 2011 05:05:50 GMT</pubDate>
</item>
<item>
<title>Firefox加速版大評比(1):前言</title>
<link>http://ithelp.ithome.com.tw/question/10076045?tag=rss.qu</link>
<description>想當年Firefox之前在全盛時期,可以在mozillaZine論壇找到一卡車自行編譯的加速版本。如今雖然不再那麼風光,被Google Chrome吃下了不少市占,身邊不少朋友跳糟Chrome陣營,不過使用Firefox的好處隨著記憶體越來越便宜,仍然是個很好很強大的資訊平台。而社群版本能夠以處理器指令等優化,讓Firefox更有效率,你用的是那一家的社群版本呢?</description>
<author>xdriftdoll</author>
<pubDate>Thu, 13 Oct 2011 05:03:31 GMT</pubDate>
</item>
<item>
<title>Windows Server 2008和Hyper-V簡介</title>
<link>http://ithelp.ithome.com.tw/question/10076044?tag=rss.qu</link>
<description>介紹Windows Server 2008與Hyper-V。</description>
<author>chiounan</author>
<pubDate>Thu, 13 Oct 2011 04:22:42 GMT</pubDate>
</item></channel>
</rss>

要提供如上的 XML 資料,可以寫類似以下的程式,這裡以 php 做例子

<?php
$dbconnect = mysql_connect($server, $loginsql, $passsql) or die('Could not connect: ' . mysql_error());
mysql_select_db($base) or die('Could not select database');

mb_internal_encoding ('utf8');
mysql_query ("SET CHARACTER SET 'utf8'");
mysql_query ("SET NAMES 'utf8'");

$query="select * from rss_table ORDER BY pubdate";
$result = mysql_query($query, $dbconnect);

while ($line = mysql_fetch_assoc($result)) {
	$return[] = $line;
}

$now = date("Y-m-d\TH:i:s\Z");

$output = "<rss version=\"2.0\" encoding=\"UTF-8\">
		<channel>
		<title>iT邦幫忙</title>
		<description></description>
		<link>http://ithelp.ithome.com.tw/</link>
		<lastBuildDate>$now</lastBuildDate>
		<generator>FeedCreator 1.7.2-ppt (info@mypapit.net)</generator>
		<image>
		<url>http://ithelp.ithome.com.tw/images/logo.png</url>
		<title></title>
		<link>http://ithelp.ithome.com.tw</link>
		</image>";

foreach ($return as $line) {
   $title = $line['title'];
   $link = $line['link'];
   $description = $line['description'];
   $author = $line['author'];
   $pubdate = $line['pubdate'];

   $output .= "<item>
		<title>".$title."</title>
		<link>".$link."</link>
		<description>".$description."</description>
		<author>".$author."</author>
		<pubDate>".$pubdate."</pubDate>
	      </item>";   
}

$output .= "</channel>";
$output .= "</rss>";

header("Content-Type: application/xml");

echo $output;

mysql_close($dbconnect);	
?>

程式說明如下,雖然是 php 的語法,但是換成別的程式語言,相信也是大同小異。
1.準備資料庫環境
列 2 至列 7
2.讀資料表
列 9 至列 14,此程式將資料存到一個陣列,一般情形使用 recordset/resultset 什 麼 set 都可以
3.起始結果字串
列 18 至列 29 ,將固定的字串部份存到一個字串變數中
4.加入結果字串
列 31 至列 45 ,逐筆讀取資料,並組成 XML 的型式後,加到步驟 3 的字串變數 中,在此例中,很明顯地可以看出,會重覆 Loop 的是從 <item> 到 </item> 這個 部份,也可合理的推測,這個部份是由資料庫中抓到的
5.收尾結果字串
列 47 至列 48 ,這個部份要注意其「有開始就要有結束」的兩兩對應關係,否則就 會是一份不合規定的 XML 文件。
6.設定 content-type
列 50 ,這是為了要與其他資料型態的 HTTP Response 做區隔,固定的標準用字 是 application/xml。
7.輸出結果字串
列 52
8.斷開資料庫連接,收工放飯
列 54

本系列文章列表
Next : [XML]27-建置Client端之XMLHttpReqeust


上一篇
[XML]25-文件之特殊資料
下一篇
[XML]27-建置Client端之XMLHttpReqeust
系列文
XML30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
鐵殼心
iT邦高手 1 級 ‧ 2011-10-23 00:21:46

看不懂也請鼓掌
拍手

krarm iT邦好手 1 級 ‧ 2011-10-23 01:55:07 檢舉

推就對了

0
Ken(Bigcandy)
iT邦大師 1 級 ‧ 2011-10-23 03:48:08

推+1,哈哈

0
modernsarah
iT邦研究生 4 級 ‧ 2011-10-23 15:15:49

看不懂,也憑感覺推就對了。
讚

我要留言

立即登入留言