為了幫女朋友寫一個XML的讀寫工具所以今天花了不少時間,
又因為是第二次寫PHP所以一些基本的語法老摸不清楚,每種語言的特性真的差異不小。
像是語法的宣告、陣列的操作、CLASS的結構、參數的存取...ext 都有很大的差異。
但是基本上後來發現PHP有很多部分都很接近C,像是sizeof、函示呼叫方式都很像,可能PHP的作者寫C寫很久吧!
不過寫過JAVA、DOT NET、C、C++、VB後我發現還是SCRIPT LANGUAGE簡單多了!
也因為可以很快的上手(像一天可以入門)進而可以很快的得到成就感。
EMBEDDED就沒辦法了,一個小小的測試都需要花上很多的時間,OK!不費話。
這個XML 的CLASS用了XMLReader來做,主要是讀取一個XML檔案,將檔案內的Entity解讀出來然後加入到一個陣列中再回傳給UI LAYER使用。因此這是個UTILITY CLASS。
檔案的範例:
PHP的程式碼:
header("Content-type: text/html;charset=utf8");
// 儲存單筆資料的物件
class CouponEnt{
var $name;
var $period;
var $photo;
var $pic;
var $brand;
var $category;
}
// 讀取XML並把資料轉為ARRAY
class CouponBank
{
private $file_name;
private $xml_reader;
private $ent_bank;
public static $test = "test
";
";
private $top_node = "coupon";
public function __construct($file_name)
{
$this->file_name = $file_name;
$this->xml_reader = new XMLReader();
$this->ent_bank = array();
$this->loadFile($this->file_name);
$this->parseFile($this->xml_reader);
}
// load file
private function loadFile($file)
{
$this->xml_reader->open($file);
}
// parset xml
private function parseFile($xml_reader)
{
while($xml_reader->read())
{
if($xml_reader->nodeType == XMLReader::ELEMENT && $xml_reader->name == $this->top_node)
{
$coupon = $this->getElement($xml_reader);
array_push($this->ent_bank, $coupon);
}
}
return $ent_bank;
}
// translate xml element to object
private function getElement($xml_reader)
{
$coupon = new CouponEnt();
while(true)
{
// stop loop on tag end
if($xml_reader->nodeType == XMLReader::END_ELEMENT && $xml_reader->name=="coupon")
{
break;
}
// set element value to entity
if($xml_reader->nodeType == XMLReader::ELEMENT)
{
switch($xml_reader->name)
{
case "name":
$xml_reader->read();
$coupon->name = $xml_reader->value;
$xml_reader->read();
break;
case "period":
$xml_reader->read();
$coupon->period = $xml_reader->value;
$xml_reader->read();
break;
case "photo":
$xml_reader->read();
$coupon->photo = $xml_reader->value;
$xml_reader->read();
break;
case "pic":
$xml_reader->read();
$coupon->pic = $xml_reader->value;
$xml_reader->read();
break;
case "brand":
$xml_reader->read();
$coupon->brand = $xml_reader->value;
$xml_reader->read();
break;
case "category":
$xml_reader->read();
$coupon->category = $xml_reader->value;
$xml_reader->read();
break;
default:
$xml_reader->read();
break;
}
}
else
{
$xml_reader->read();
}
}
return $coupon;
}
public function getCoupons()
{
return $this->ent_bank;
}
public function release()
{
$this->xml_reader->close();
$this->xml_reader = null;
}
public function debug()
{
if(sizeof($this->ent_bank)<=0)
echo "debug : element array is empty.
";
";
else
{
print_r($this->ent_bank);
}
}
}
// 建立新的物件
$bank = new CouponBank("data.xml");
// 讀取內容,回傳的是陣列,內容物是CouponEnt
$cps = $bank->getCoupons();
for($i = 0; $i <>
{
echo " =================== [Coupon information]
";
";
echo " name = ".$cps[$i]->name."
";
";
echo " period = ".$cps[$i]->period."
";
";
echo " photo = ".$cps[$i]->photo."
";
";
echo " pic = ".$cps[$i]->pic."
";
";
echo " brand = ".$cps[$i]->brand."
";
";
echo " category = ".$cps[$i]->category."
";
";
echo " =======================================
";
";
}
$bank->release();
//$bank->debug();
?>
沒有留言:
張貼留言