PHP5對象simplexml的詳細分析
PHP5對象simplexml是一個新增的針對XML的一個對象。我們將會在這篇文章中對PHP5對象simplexml的屬性和方法進行詳細的介紹,希望大家能夠通過本文對這新增對象有一個深刻的了解。
#t#simplexml_load_file(str file); 將一個xml文檔載入一個simplexml對象中,此方法返回一個句柄
simplexml_load_string(str string);將一段xml文檔載入一個simplexml對象中,此方法返回一個句柄
simplexml_import_dom(data) 該函數(shù)把一個dom節(jié)點轉(zhuǎn)為simplexml對象,其中data為實用的DOM節(jié)點
$simplexml->addAttribute(name,value) 給simplexml對象元素添加一個屬性,注意,是simplexml對象元素,不是simplexml對象句柄。
$simplexml->addChlid(name,value)給指定的XML節(jié)點添加一個子節(jié)點
$simplexml->asXML() 從SimpleXMLElement對象中以一個字符串的形式返回XML文檔,可以當作保存xml文檔來來用
$simplexml->attributes() 返回由PHP5對象simplexml元素節(jié)點的屬性組成的數(shù)組,
運行上面代碼會打印出以個數(shù)組,Array ( [@attributes] => Array ( [name] => cx,html [tt] => ddd ) )
可見,$simplexml->attributes()得到的是一個xml元素節(jié)點的所有屬性,不過被包含在一個[@attributes]數(shù)組中,所以要通過$xml = $xml['@attributes']來獲得
$simplexml->Children() 返回simplexml對象元素節(jié)點的孩子組成的數(shù)組
$simplexml->__construct() 創(chuàng)建一個新的XML文檔
$simplexml->getDocNamespaces() 該函數(shù)返回$simplexml對象定義的命名空間
$simplexml->getName() 返回$simplexml對象元素的名稱,即標簽名
$simplexml->getNamespaces() 返回$simplexml對象使用的命名空間
$simplexml->registerXpathNamespace() 該函數(shù)為下一次XPATH查詢創(chuàng)建命名空間語境
$simplexml->xpath() 使用xpath的語法來解析一個PHP5對象simplexml
實例代碼1
- <?xml version="1.0" encoding="gbk"?>
- <LeapsoulXML>
- <LeapsoulInfo>
- <name>Leapsoul-PHP網(wǎng)站開發(fā)</name>
- <website>http://www.leapsoul.cn</website>
- <description>分享PHP網(wǎng)站開發(fā)與建設(shè)的樂趣,教你如何建立網(wǎng)站</description>
- <bloger>David</bloger>
- <date>2009-05-13</date>
- <qq>QQ:154130270</qq>
- </LeapsoulInfo>
- <LeapsoulInfo>
- <name>Leapsoul-PHP網(wǎng)站開發(fā)</name>
- <website>http://www.leapsoul.cn</website>
- <description>分享PHP網(wǎng)站開發(fā)與建設(shè)的樂趣,教你如何建立網(wǎng)站</description>
- <bloger>David</bloger>
- <date>2009-05-13</date>
- <qq>QQ:154130270</qq>
- </LeapsoulInfo>
- </LeapsoulXML>
我們可以結(jié)合上面的PHP5對象simplexml示例,再加上自己的了解,應(yīng)該能夠充分的認識這一新增的函數(shù)。