標籤:des style blog http io ar color os 使用
XML簡介
XML是一種流行的半結構化檔案格式,以一種類似資料庫的格式儲存資料。在實際應用中,一些簡單的、安全性較低的資料往往使用 XML檔案的格式進行儲存。這樣做的好處一方面可以通過減少與資料庫的互動性操作提高讀取效率,另一方面可以有效利用 XML的優越性降低程式的編寫難度。
PHP提供了一整套的讀取 XML檔案的方法,很容易的就可以編寫基於 XML的指令碼程式。本章將要介紹 PHP與 XML的操作方法,並對幾個常用的 XML類庫做一些簡要介紹。
1 XML簡介
XML是“可擴充性標識語言(eXtensible Markup Language)”的縮寫,是一種類似於 HTML的標記性語言。但是與 HTML不同,XML主要用於描述資料和存放資料,而 HTML主要用於顯示資料。
php操作xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- Edited with XML Spy v2007 (http://www.altova.com) --> 3 <breakfast_menu> 4 <food> 5 <name>Belgian Waffles</name> 6 <price>$5.95</price> 7 <description>two of our famous Belgian Waffles with plenty of real maple syrup</description> 8 <calories>650</calories> 9 </food>10 <food>11 <name>Strawberry Belgian Waffles</name>12 <price>$7.95</price>13 <description>light Belgian waffles covered with strawberries and whipped cream</description>14 <calories>900</calories>15 </food>16 <food>17 <name>Berry-Berry Belgian Waffles</name>18 <price>$8.95</price>19 <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>20 <calories>900</calories>21 </food>22 <food>23 <name>French Toast</name>24 <price>$4.50</price>25 <description>thick slices made from our homemade sourdough bread</description>26 <calories>600</calories>27 </food>28 <food>29 <name>Homestyle Breakfast</name>30 <price>$6.95</price>31 <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>32 <calories>950</calories>33 </food>34 </breakfast_menu>
simple.xml檔案
1.建立一個 SimpleXML對象
$xml=simplexml_load_file(‘simple.xml‘,‘SimpleXMLElement‘);
2.讀出xml內容
foreach($xml->food as $v){ echo $v->name."---".$v->description."---". $v->price."<br/>";}3.增加節點
1 $xml->addChild("name","value");4.增加節點屬性
$son=$xnl->addChild("name","value");$son->addAttibute("name","value");//name表示屬性名稱 value 表示屬性值5.更新xml檔案
$content=$xml->saveXML();$fp=fopen(‘simple.xml‘,‘wb+‘);fwrite($fp,$content);fclose($fp);
php simpleXML操作xml的用法