乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      php simplexmlElement 操作xml的命名空間

       corefashion 2015-07-14

      這是今天中午發(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)大致如下:

      XML/HTML代碼
      1. <feed xmlns='http://www./2005/Atom'      xmlns:openSearch='http:///-/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."'>      
      2.     <id>liz@gmail.com</id>      
      3.     <updated>2008-12-10T10:04:15.446Z</updated>      
      4.     <category scheme='http://schemas.google.com/g/2005#kind'      term='http://schemas.google.com/contact/2008#contact' />      
      5.     <title>Elizabeth Bennet's Contacts</title>      
      6.     <link rel='http://schemas.google.com/g/2005#feed'      type='application/atom+xml'       />      
      7.     <link rel='http://schemas.google.com/g/2005#post'      type='application/atom+xml'       />      
      8.     <link rel='http://schemas.google.com/g/2005#batch'      type='application/atom+xml'       />      
      9.     <link rel='self' type='application/atom+xml'       />      
      10.     <author>        
      11.         <name>Elizabeth Bennet</name>        
      12.         <email>liz@gmail.com</email>      
      13.     </author>      
      14.     <generator version='1.0' uri='http://www.google.com/m8/feeds'>      Contacts    </generator>      
      15.     <openSearch:totalResults>1</openSearch:totalResults>      
      16.     <openSearch:startIndex>1</openSearch:startIndex>      
      17.     <openSearch:itemsPerPage>25</openSearch:itemsPerPage>      
      18.     <entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'>        
      19.         <id>        http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de      </id>        
      20.         <updated>2008-12-10T04:45:03.331Z</updated>        
      21.         <app:edited xmlns:app='http://www./2007/app'>2008-12-10T04:45:03.331Z</app:edited>        
      22.         <category scheme='http://schemas.google.com/g/2005#kind'        term='http://schemas.google.com/contact/2008#contact' />        
      23.         <title>Fitzwilliam Darcy</title>        
      24.         <gd:name>          
      25.             <gd:fullName>Fitzwilliam Darcy</gd:fullName>        
      26.         </gd:name>        
      27.         <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'         />        
      28.         <link rel='self' type='application/atom+xml'         />        
      29.         <link rel='edit' type='application/atom+xml'         />        
      30.         <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'        primary='true'>        456      </gd:phoneNumber>        
      31.         <gd:extendedProperty name='pet' value='hamster' />        
      32.         <gContact:groupMembershipInfo deleted='false'         />      
      33.     </entry>    
      34. </feed>  

      這個(gè)結(jié)構(gòu)在上面的地址里有,這個(gè)是我格式化過(guò)的XML數(shù)據(jù),現(xiàn)在要取得類(lèi)似于“<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'        primary='true'>        456      </gd:phoneNumber> ”中的值。

      最終代碼如下:

      PHP代碼
      1. $x = new SimpleXmlElement($str);  
      2. foreach($x->entry as $t){  
      3.     echo $t->id . "<br >";  
      4.     echo $t->updated . "<br />";  
      5.     $namespaces = $t->getNameSpaces(true);  
      6.     $gd = $t->children($namespaces['gd']);   
      7.     echo $gd->phoneNumber;  
      8. }  

      當(dāng)然,如果不象上面這樣寫(xiě),也可以寫(xiě)成這樣:

      PHP代碼
      1. $x = new SimpleXmlElement($str);  
      2. foreach($x->entry as $t){  
      3.     echo $t->id . "<br >";  
      4.     echo $t->updated . "<br />";  
      5.     //$namespaces = $t->getNameSpaces(true);  
      6.     //注意這里與上面一段的區(qū)別  
      7.     $gd = $t->children('http://schemas.google.com/g/2005');   
      8.     echo $gd->phoneNumber;  
      9. }  

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

      XML/HTML代碼
      1. <event:event>   
      2. <event:sessionKey></event:sessionKey>   
      3. <event:sessionName>Learn QB in Minutes</event:sessionName>   
      4. <event:sessionType>9</event:sessionType>   
      5. <event:hostWebExID></event:hostWebExID>   
      6. <event:startDate>02/12/2009</event:startDate>   
      7. <event:endDate>02/12/2009</event:endDate>   
      8. <event:timeZoneID>11</event:timeZoneID>   
      9. <event:duration>30</event:duration>   
      10. <event:description></event:description>   
      11. <event:status>NOT_INPROGRESS</event:status>   
      12. <event:panelists></event:panelists>   
      13. <event:listStatus>PUBLIC</event:listStatus>   
      14. </event:event>  

      這種非標(biāo)準(zhǔn)的XML,沒(méi)有定義命名空間,怎么辦?在這種情況下,其實(shí)SimpleXmlElement就已經(jīng)直接可以解決了,但是會(huì)報(bào)warnging,因?yàn)樗J(rèn)為event這個(gè)命名空間不存在。
      解決方法是:

      PHP代碼
      1. $xml = @new SimpleXmlElement($str);//在前面加@抑止錯(cuò)誤。  
      2. echo "<pre>";  
      3. print_r($xml);  

      目前看來(lái),這種解決方法比較好。


      轉(zhuǎn)發(fā)到朋友圈:



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多