星期六, 4月 17, 2010

PHP SimpleXML用法之一

    閱讀看本節之前應該要先了解XML無廢話網站中,那一個PDF檔的內容,寫得真是感人肺腑,文章應該是10年前寫的,真的非常不簡單。只是XML Schema還需要自己看看別的資料。
    既然已經有文件說的這麼清楚了,應該直接進入SimpleXML即可,PHP有許多存取XML的套件,但是操作難度各有不同,而公認最簡單的應該就是SimpleXML。
先來看一個XML的範例:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <firstname>Sheila</firstname>
    <surname>Green</surname>
    <address>2 Good St</address>
    <city>Campbelltown</city>
    <country>Australia</country>
    <contact>
      <phone>1234 1234</phone>
      <url>http://example.com</url>
      <email>pamela@example.com</email>
    </contact>
    </user>
    <user>
    <firstname>Bruce</firstname>
    <surname>Smith</surname>
    <address>1 Yakka St</address>
    <city>Meekatharra</city>
    <country>Australia</country>
    <contact>
      <phone>4444 4444</phone>
      <url>http://yakka.example.com</url>
      <email>bruce@yakka.example.com</email>
    </contact>
  </user>
  <user>
    <firstname>Davo</firstname>
    <surname>White</surname>
    <address>The Only Way</address>
    <city>Whitefall</city>
    <country>Australia</country>
    <contact>
      <phone>8888 8888</phone>
      <url>http://white.example.com</url>
      <email>davo@white.example.com</email>
    </contact>
  </user>
  <user>
    <firstname>Shazza</firstname>
    <surname>Green</surname>
    <address>222 Great Western Hwy</address>
    <city>Bathurst</city>
    <country>Australia</country>
    <contact>
      <phone>6666 6666</phone>
      <url>http://shazza.green.example.com</url>
      <email>shazza@green.example.com</email>
    </contact>
  </user>
</users>
這是一個XML包含了通訊錄的資料。而如何使用SimpleXML將資料取出、再利用。將是
本結要實做的重點。
實做順序如下:
About 1. 載入XML檔
    example 1. 由外部檔案載入   
<?php

    if( ! $xml = simplexml_load_file('address.xml') )
    {
        echo 'unable to load XML file';
    }
    else
    {
        echo 'XML file loaded successfully';
    }
?> 
 
exmaple 2. 由字串載入
<?php
$xml_string = '<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <firstname>Sheila</firstname>
    <surname>Green</surname>
    <address>2 Good St</address>
    <city>Campbelltown</city>
    <country>Australia</country>
    <contact>
      <phone>1234 1234</phone>
      <url>http://example.com</url>
      <email>pamela@example.com</email>
    </contact>
    </user>
</users>';

    if( ! $xml = simplexml_load_string( $xml_string ) )
    {
        echo 'Unable to load XML string';
    }
    else
    {
        echo 'XML String loaded successfully';
    }

?> 
理用上述方法將XML載入後,接下來便是要取得各個節點以便可以將資料做相關利用。
About 2. 載入結點資料
   
example 1: 一般方式 
<?php

    if( ! $xml = simplexml_load_file('address.xml') )
    {
        echo 'unable to load XML file';
    }
    else
    {
        foreach( $xml as $user )
        {
            echo 'Firstname: '.$user->firstname.'<br />';
            echo 'Surname: '.$user->surname.'<br />';
            echo 'Address: '.$user->address.'<br />';
            echo 'City: '.$user->city.'<br />';
            echo 'Country: '.$user->country.'<br />';
            echo 'Email: '.$user->contact->phone.'<br />';
            echo 'Email: '.$user->contact->url.'<br />';
            echo 'Email: '.$user->contact->email.'<br />';
        }
    }

?> 
 
example 2. 以DOM形式
<?php
$xml_string = '<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <firstname>Sheila</firstname>
    <surname>Green</surname>
    <address>2 Good St</address>
    <city>Campbelltown</city>
    <country>Australia</country>
    <contact>
      <phone>1234 1234</phone>
      <url>http://example.com</url>
      <email>pamela@example.com</email>
    </contact>
    </user>
</users>';

/*** a new DOM object ***/
$dom = new DOMDocument;

/*** load the XML string ***/
$dom->loadXML( $xml_string );
$sxe = simplexml_import_dom($dom);

echo $sxe->user[0]->surname;
?>
藉由利用DOMDocument的建構一物件後載入XML檔,然後import DOM便可以將節點取出。
                                                                                            下一頁
                                                                                         update : 2010/3/24

沒有留言:

張貼留言