激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php simplexmlElement操作xml的命名空間實現代碼

php simplexmlElement操作xml的命名空間實現代碼

2019-11-15 14:04php教程網 PHP教程

這是今天中午發生的事情,有人在群里求助,比如xml中如果標記是content這樣的情況下,取不到 xx:xxxx 為下標的值。

看了這個問題,第一個反應就是namespace的關系,但我從來沒有使用simplexml操作過namespace,于是就翻開手冊查了一下資料,問題并沒有解決,最終是通過google解決了該問題。 

提問題的朋友貼出了數據源,來自于:http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query,數據結構大致如下: 

復制代碼代碼如下:


<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>[email protected]</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' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' /> 
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' /> 
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/batch' /> 
<link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full?max-results=25' /> 
<author> 
<name>Elizabeth Bennet</name> 
<email>[email protected]</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/*' href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' /> 
<link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' /> 
<link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' /> 
<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' href='http://www.google.com/m8/feeds/groups/liz%40gmail.com/base/270f' /> 
</entry> 
</feed> 


這個結構在上面的地址里有,這個是我格式化過的XML數據,現在要取得類似于“<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; 


當然,如果不象上面這樣寫,也可以寫成這樣: 

復制代碼代碼如下:


$x = new SimpleXmlElement($str); 
foreach($x->entry as $t){ 
echo $t->id . "<br >"; 
echo $t->updated . "<br />"; 
//$namespaces = $t->getNameSpaces(true); 
//注意這里與上面一段的區別 
$gd = $t->children('http://schemas.google.com/g/2005'); 
echo $gd->phoneNumber; 


只是象第二種寫法就屬于硬編碼了,這樣不太好,萬一哪天有變化,還得再更改N多代碼。 
問題接踵而來,比如象下面這段: 

復制代碼代碼如下:


<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> 


這種非標準的XML,沒有定義命名空間,怎么辦?在這種情況下,其實SimpleXmlElement就已經直接可以解決了,但是會報warnging,因為他認為event這個命名空間不存在。 
解決方法是: 

復制代碼代碼如下:


$xml = @new SimpleXmlElement($str);//在前面加@抑止錯誤。 
echo "<pre>"; 
print_r($xml); 


目前看來,這種解決方法比較好。 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本不卡一区二区三区在线观看 | 福利在线国产 | 久久99亚洲精品 | 在线看免电影网站 | 久久草在线视频 | 亚州综合一区 | 国产高清美女一级毛片久久 | 日韩精品中文字幕在线播放 | av在线播放免费 | 免费黄色大片在线观看 | 免费日本一区二区 | 毛片a片免费看 | 欧美一级毛片美99毛片 | 久久久久国产精品久久久久 | 黄色大片在线免费观看 | 一级毛片在线观看免费 | 久久久婷婷 | 日韩精品中文字幕在线观看 | 亚洲国产女同久久 | 亚洲一区在线观看视频 | 毛片大全在线观看 | 4p一女两男做爰在线观看 | 羞羞视频免费观看入口 | 全免费午夜一级毛片真人 | 精品成人国产在线观看男人呻吟 | 国产91久久精品 | 中文字幕专区高清在线观看 | 91短视频在线播放 | 国产日韩欧美 | av之家在线观看 | 青草伊人网 | 国产激情网| 国产免费人做人爱午夜视频 | 欧美韩国日本在线 | 国产亚洲精品久久久久婷婷瑜伽 | 毛片国产 | 一本色道久久综合亚洲精品图片 | 永久免费不卡在线观看黄网站 | 日本高清在线免费 | 黑人日比| 一区二区三区欧洲 |