php simplexmlElement操作xml的命名空間實(shí)現(xiàn)代碼
更新時(shí)間:2011年01月04日 22:43:48 作者:
這是今天中午發(fā)生的事情,有人在群里求助,比如xml中如果標(biāo)記是<xx:xxxx>content</xx:xxxx>這樣的情況下,取不到 xx:xxxx 為下標(biāo)的值。
看了這個(gè)問(wèn)題,第一個(gè)反應(yīng)就是namespace的關(guān)系,但我從來(lái)沒(méi)有使用simplexml操作過(guò)namespace,于是就翻開(kāi)手冊(cè)查了一下資料,問(wèn)題并沒(méi)有解決,最終是通過(guò)google解決了該問(wèn)題。
提問(wèn)題的朋友貼出了數(shù)據(jù)源,來(lái)自于:http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query,數(shù)據(jù)結(jié)構(gòu)大致如下:
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'>
<id>liz@gmail.com</id>
<updated>2008-12-10T10:04:15.446Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<title>Elizabeth Bennet's Contacts</title>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' />
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' />
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' />
<link rel='self' type='application/atom+xml' />
<author>
<name>Elizabeth Bennet</name>
<email>liz@gmail.com</email>
</author>
<generator version='1.0' uri='http://www.google.com/m8/feeds'> Contacts </generator>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'>
<id> http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de </id>
<updated>2008-12-10T04:45:03.331Z</updated>
<app:edited xmlns:app='http://www.w3.org/2007/app'>2008-12-10T04:45:03.331Z</app:edited>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<title>Fitzwilliam Darcy</title>
<gd:name>
<gd:fullName>Fitzwilliam Darcy</gd:fullName>
</gd:name>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' />
<link rel='self' type='application/atom+xml' />
<link rel='edit' type='application/atom+xml' />
<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber>
<gd:extendedProperty name='pet' value='hamster' />
<gContact:groupMembershipInfo deleted='false' />
</entry>
</feed>
這個(gè)結(jié)構(gòu)在上面的地址里有,這個(gè)是我格式化過(guò)的XML數(shù)據(jù),現(xiàn)在要取得類似于“<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber> ”中的值。
最終代碼如下:
$x = new SimpleXmlElement($str);
foreach($x->entry as $t){
echo $t->id . "<br >";
echo $t->updated . "<br />";
$namespaces = $t->getNameSpaces(true);
$gd = $t->children($namespaces['gd']);
echo $gd->phoneNumber;
}
當(dāng)然,如果不象上面這樣寫(xiě),也可以寫(xiě)成這樣:
$x = new SimpleXmlElement($str);
foreach($x->entry as $t){
echo $t->id . "<br >";
echo $t->updated . "<br />";
//$namespaces = $t->getNameSpaces(true);
//注意這里與上面一段的區(qū)別
$gd = $t->children('http://schemas.google.com/g/2005');
echo $gd->phoneNumber;
}
只是象第二種寫(xiě)法就屬于硬編碼了,這樣不太好,萬(wàn)一哪天有變化,還得再更改N多代碼。
問(wèn)題接踵而來(lái),比如象下面這段:
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
這種非標(biāo)準(zhǔn)的XML,沒(méi)有定義命名空間,怎么辦?在這種情況下,其實(shí)SimpleXmlElement就已經(jīng)直接可以解決了,但是會(huì)報(bào)warnging,因?yàn)樗J(rèn)為event這個(gè)命名空間不存在。
解決方法是:
$xml = @new SimpleXmlElement($str);//在前面加@抑止錯(cuò)誤。
echo "<pre>";
print_r($xml);
目前看來(lái),這種解決方法比較好。
PHP SimpleXML 函數(shù) 相關(guān)資料
http://www.fzitv.net/w3school/php/php_ref_simplexml.htm
PHP SimpleXML
http://www.fzitv.net/w3school/php/php_xml_simplexml.htm
提問(wèn)題的朋友貼出了數(shù)據(jù)源,來(lái)自于:http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query,數(shù)據(jù)結(jié)構(gòu)大致如下:
復(fù)制代碼 代碼如下:
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'>
<id>liz@gmail.com</id>
<updated>2008-12-10T10:04:15.446Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<title>Elizabeth Bennet's Contacts</title>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' />
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' />
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' />
<link rel='self' type='application/atom+xml' />
<author>
<name>Elizabeth Bennet</name>
<email>liz@gmail.com</email>
</author>
<generator version='1.0' uri='http://www.google.com/m8/feeds'> Contacts </generator>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'>
<id> http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de </id>
<updated>2008-12-10T04:45:03.331Z</updated>
<app:edited xmlns:app='http://www.w3.org/2007/app'>2008-12-10T04:45:03.331Z</app:edited>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<title>Fitzwilliam Darcy</title>
<gd:name>
<gd:fullName>Fitzwilliam Darcy</gd:fullName>
</gd:name>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' />
<link rel='self' type='application/atom+xml' />
<link rel='edit' type='application/atom+xml' />
<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber>
<gd:extendedProperty name='pet' value='hamster' />
<gContact:groupMembershipInfo deleted='false' />
</entry>
</feed>
這個(gè)結(jié)構(gòu)在上面的地址里有,這個(gè)是我格式化過(guò)的XML數(shù)據(jù),現(xiàn)在要取得類似于“<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber> ”中的值。
最終代碼如下:
復(fù)制代碼 代碼如下:
$x = new SimpleXmlElement($str);
foreach($x->entry as $t){
echo $t->id . "<br >";
echo $t->updated . "<br />";
$namespaces = $t->getNameSpaces(true);
$gd = $t->children($namespaces['gd']);
echo $gd->phoneNumber;
}
當(dāng)然,如果不象上面這樣寫(xiě),也可以寫(xiě)成這樣:
復(fù)制代碼 代碼如下:
$x = new SimpleXmlElement($str);
foreach($x->entry as $t){
echo $t->id . "<br >";
echo $t->updated . "<br />";
//$namespaces = $t->getNameSpaces(true);
//注意這里與上面一段的區(qū)別
$gd = $t->children('http://schemas.google.com/g/2005');
echo $gd->phoneNumber;
}
只是象第二種寫(xiě)法就屬于硬編碼了,這樣不太好,萬(wàn)一哪天有變化,還得再更改N多代碼。
問(wèn)題接踵而來(lái),比如象下面這段:
復(fù)制代碼 代碼如下:
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
這種非標(biāo)準(zhǔn)的XML,沒(méi)有定義命名空間,怎么辦?在這種情況下,其實(shí)SimpleXmlElement就已經(jīng)直接可以解決了,但是會(huì)報(bào)warnging,因?yàn)樗J(rèn)為event這個(gè)命名空間不存在。
解決方法是:
復(fù)制代碼 代碼如下:
$xml = @new SimpleXmlElement($str);//在前面加@抑止錯(cuò)誤。
echo "<pre>";
print_r($xml);
目前看來(lái),這種解決方法比較好。
PHP SimpleXML 函數(shù) 相關(guān)資料
http://www.fzitv.net/w3school/php/php_ref_simplexml.htm
PHP SimpleXML
http://www.fzitv.net/w3school/php/php_xml_simplexml.htm
您可能感興趣的文章:
- PHP XML操作類DOMDocument
- php中DOMDocument簡(jiǎn)單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP使用DOMDocument類生成HTML實(shí)例(包含常見(jiàn)標(biāo)簽元素)
- PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問(wèn)題的解決方案
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建XML文檔的方法
- PHP基于DOM創(chuàng)建xml文檔的方法示例
- PHP創(chuàng)建XML接口示例
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
相關(guān)文章
php設(shè)計(jì)模式 Decorator(裝飾模式)
動(dòng)態(tài)的給一個(gè)對(duì)象添加一些額外的職責(zé),就擴(kuò)展功能而言比生成子類方式更為靈活2011-06-06
PHP實(shí)現(xiàn)websocket通信的方法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)websocket通信的方法,結(jié)合實(shí)例形式分析了php基于websocket類的socket通信相關(guān)客戶端與服務(wù)器端操作技巧,需要的朋友可以參考下2018-08-08
PHP用戶管理中常用接口調(diào)用實(shí)例及解析(含源碼)
本文主要介紹了PHP用戶管理中常用接口調(diào)用實(shí)例及解析(含源碼),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
PHP管理依賴(dependency)關(guān)系工具 Composer的自動(dòng)加載(autoload)
Composer 是PHP的一個(gè)包依賴管理工具,類似Ruby中的RubyGems或者Node中的NPM,它并非官方,但現(xiàn)在已經(jīng)非常流行。此文并不介紹如何使用Composer,而是關(guān)注于它的autoload的內(nèi)容吧。2014-08-08
一個(gè)PHP緩存類代碼(附詳細(xì)說(shuō)明)
一個(gè)PHP緩存類代碼,后面都有詳細(xì)的說(shuō)明,學(xué)習(xí)php的朋友可以參考下。2011-06-06
PHP圖像處理類庫(kù)MagickWand用法實(shí)例分析
這篇文章主要介紹了PHP圖像處理類庫(kù)MagickWand用法,較為詳細(xì)的分析了php中圖像處類庫(kù)MagickWand的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
關(guān)于PHP中協(xié)程和阻塞的一些理解與思考
這篇文章主要給大家介紹了關(guān)于PHP中協(xié)程和阻塞的一些理解與思考,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08

