Moya's Blog

About

慈濟大陸賑災、緬甸風災捐款: External link mark
郵政劃撥帳號-06692433
戶名-佛教慈濟基金會賑災專戶
(註明四川震災或緬甸風災)
慈濟捐款網站請按此 External link mark


處順境若能慈悲,則後福自在,仆逆境倘得智慧,則遺禍自消,於順境若能不喜,則後患不至,當逆境或能捨得,則福自踵來。此慈悲喜捨,便是大般涅槃無上妙法,入世行者或能三思。


《既然清淨,何必有網?》
《譬如大日,遍照不爽。》
《上下十方,無為自在;》
《因陀羅手,去曼達礙。》


靜思晨語系列 獅子吼大德 慈濟的好友 雅虎的好友 老骨頭級的朋友

12 May 2008 - 14:03 in by MoyaTseng
上次介紹如何用 PHP 的 SimpleXML 讀取一個既有的 XML 檔案,並且將這個內容轉成 array。這一次來介紹怎樣利用 SimpleXML 來建立一個全新的 XML 檔案。

簡易產出 XML 文檔的程式撰寫說明

函示使用說明

  1. 建立一個基礎的 XML 文檔
    $body = "<?xml version='1.0' standalone='yes'?>
    <files>
    </files>";
    $sxe = new SimpleXMLElement($body);
    
  2. 增加屬性
    $sxe->addAttribute(type, configuration);
    
  3. 增加子節點
    $movie = $sxe->addChild(node);
    
  4. 增加子節點的物件
    $movie->addChild(key, value);
    
  5. 輸出既有的 XML 內容
    echo $sxe->asXML();
    

簡單的範例程式

<?php

 $sxe = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?>
<files>
</files>");
 $sxe->addAttribute('type', 'documentary');

 $movie = $sxe->addChild('file');
 $movie->addChild('snapshot', 'file-1.jpg');
 $movie->addChild('movie', 'file-1.wmv');

 $movie = $sxe->addChild('file');
 $movie->addChild('snapshot', 'file-2.jpg');
 $movie->addChild('movie', 'file-2.wmv');

 echo $sxe->asXML();

?>

程式執行結果

<?xml version="1.0" standalone="yes"?>
<files type="documentary">
  <file>
    <snapshot>file-1.jpg</snapshot>
    <movie>file-1.wmv</movie>
  </file>
  <file>
    <snapshot>file-2.jpg</snapshot>
    <movie>file-2.wmv</movie>
  </file>
</files>


Leave a Reply

You may have to login or register to comment if you haven't already.

訪客統計: 4336 人次



請按此訂閱每日人間菩提。靜思晨語 External link mark
本站所有言論均不代表慈濟基金會 External link mark


我為你祝福
我也要許願

r1 – 12 May 2008 – 15:00:38 – Main.MoyaTseng
Copyright © 1999-2009 by the contributing authors. All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems regarding Moya's Blog? Send feedback.