提問題的朋友貼出了數據源,來自于:http://code.goog " /> 国产精品久久久久影院,欧美高清 videos sexo,成年人视频免费在线观看

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

看了這個問題,第一個反應就是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' />
<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>[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/*' 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>

這個結構在上面的地址里有,這個是我格式化過的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);

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

php SimpleXML 函數 相關資料
http://www.jb51.NET/w3school/php/php_ref_simplexml.htm
php SimpleXML
http://www.jb51.NET/w3school/php/php_xml_simplexml.htm

php技術php simplexmlElement操作xml的命名空間實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产视频精品在线偷拍 | 亚洲无人区码二码三码区别图 | 久久夜色噜噜噜亚洲AV0000 | 99久久国产露脸精品麻豆 | 久久影院一区 | 国内精品免费视频精选在线观看 | 国产一区内射最近更新 | 阿离被扒开双腿疯狂输出 | 婷婷久久综合九色综合伊人色 | 久久精品AV一区二区无码 | 欧美日韩在线成人看片a | 尹人久久大香找蕉综合影院 | 第九色区av天堂 | 伦理片在线线手机版韩国免费观看 | 我的好妈妈BD免费观看 | 某上海少妇3P黑人完整版BD | 国产乱人视频在线观看 | 性做久久久久久久久浪潮 | 精品熟女少妇AV免费观看 | 国产成+人欧美+综合在线观看 | 10分钟免费观看视频 | 在线涩涩免费观看国产精品 | 国产色婷亚洲99精品AV在 | 国产免费高清mv视频在线观看 | 妺妺窝人体色777777野大粗 | 久久热最新网站获取3 | 日本工口生肉全彩大全 | 日韩欧美一区二区中文字幕 | 国产欧美第一页 | 草柳最新地址 | 欧美另类老少配hd | 国产精品久久久久久久久爆乳 | 精品国内自产拍在线观看视频 | 暖暖 免费 高清 日本视频5 | 亚洲国产在线精品国 | 99热精品在线av播放 | 蜜桃日本MV免费观看 | 小雪奶水涨翁工帮吸的推荐语录 | 91九色精品国产免费 | 无码日韩人妻精品久久蜜桃入口 | 国产毛A片啊久久久久久A |