Moya's Blog

About

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


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


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


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

09 May 2008 - 10:45 in by MoyaTseng
一直都沒有很認真的學習 XML 的使用方式,花了一些時間稍微瞭解了一下 PHP 當中的 SimpleXML、DOM,以及 XML Parser 三種 extensions,各有巧妙不同。但是我的需求算相當低的,所以直接用 SimpleXML 即可達到目地。

簡易讀取 XML 的程式撰寫說明

需要的套件

  1. /usr/ports/lang/php5
  2. /usr/ports/textproc/php5-simplexml
  3. /usr/ports/textproc/php5-xsl

參考文件

  1. PHP.net SimpleXML Functions External link mark

函示使用說明

  1. 讀取 XML 內容
    $xml = new SimpleXMLElement($body);
    
  2. 取得屬性
    $xml->attributes();
    

簡單的範例程式

  1. 程式範例
    <?php
    
    $body = '<?xml version="1.0" encoding="UTF-8"?>
    <presence xmlns="urn:ietf:params:xml:ns:pidf"
                      entity="sip:68004810@my.hostname">
      <tuple id="68004810" callstatus="0" displayName="68004810" status="1">
        <status><basic>open</basic></status>
        <contact priority="0">sip:68004810@my.hostname</contact>
        <note>Available</note>
      </tuple>
    </presence>';
    
    $xml = new SimpleXMLElement($body);
    
    print_r ($xml);
    
    print "displayname=" . $xml->tuple->attributes()->displayName . "\n";
    print "id=" . $xml->tuple->attributes()->id . "\n";
    print "callstatus=" . $xml->tuple->attributes()->callstatus . "\n";
    print "status=" . $xml->tuple->status->basic . "\n";
    print "contact=" . $xml->tuple->contact . "\n";
    print "note=" . $xml->tuple->note . "\n";
    ?>
    
  2. 執行結果
    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [entity] => sip:68004810@my.hostname
            )
    
        [tuple] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 68004810
                        [callstatus] => 0
                        [displayName] => 68004810
                        [status] => 1
                    )
    
                [status] => SimpleXMLElement Object
                    (
                        [basic] => open
                    )
    
                [contact] => sip:68004810@my.hostname
                [note] => Available
            )
    
    )
    displayname=68004810
    id=68004810
    callstatus=0
    status=open
    contact=sip:68004810@my.hostname
    note=Available
    


Leave a Reply

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

訪客統計: 4062 人次



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


我為你祝福
我也要許願

r2 – 12 May 2008 – 15:01:18 – 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.