时间:2021-07-01 10:21:17 帮助过:5人阅读
- <br><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."'> <br><id>liz@gmail.com</id> <br><updated>2008-12-10T10:04:15.446Z</updated> <br><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> <br><title>Elizabeth Bennet's Contacts</title> <br><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' /> <br><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' /> <br><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' /> <br><link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full?max-results=25' /> <br><author> <br><name>Elizabeth Bennet</name> <br><email>liz@gmail.com</email> <br></author> <br><generator version='1.0' uri='http://www.google.com/m8/feeds'> Contacts </generator> <br><openSearch:totalResults>1</openSearch:totalResults> <br><openSearch:startIndex>1</openSearch:startIndex> <br><openSearch:itemsPerPage>25</openSearch:itemsPerPage> <br><entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'> <br><id> http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de </id> <br><updated>2008-12-10T04:45:03.331Z</updated> <br><app:edited xmlns:app='http://www.w3.org/2007/app'>2008-12-10T04:45:03.331Z</app:edited> <br><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> <br><title>Fitzwilliam Darcy</title> <br><gd:name> <br><gd:fullName>Fitzwilliam Darcy</gd:fullName> <br></gd:name> <br><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."' /> <br><link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' /> <br><link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' /> <br><gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber> <br><gd:extendedProperty name='pet' value='hamster' /> <br><gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/liz%40gmail.com/base/270f' /> <br></entry> <br></feed> <br> <br>这个结构在上面的地址里有,这个是我格式化过的XML数据,现在要取得类似于“<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber> ”中的值。 <br><br>最终代码如下: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>$x = new SimpleXmlElement($str); <br>foreach($x->entry as $t){ <br>echo $t->id . "<br >"; <br>echo $t->updated . "<br />"; <br>$namespaces = $t->getNameSpaces(true); <br>$gd = $t->children($namespaces['gd']); <br>echo $gd->phoneNumber; <br>} <br> <br>当然,如果不象上面这样写,也可以写成这样: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>$x = new SimpleXmlElement($str); <br>foreach($x->entry as $t){ <br>echo $t->id . "<br >"; <br>echo $t->updated . "<br />"; <br>//$namespaces = $t->getNameSpaces(true); <br>//注意这里与上面一段的区别 <br>$gd = $t->children('http://schemas.google.com/g/2005'); <br>echo $gd->phoneNumber; <br>} <br> <br>只是象第二种写法就属于硬编码了,这样不太好,万一哪天有变化,还得再更改N多代码。 <br>问题接踵而来,比如象下面这段: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><event:event> <br><event:sessionKey></event:sessionKey> <br><event:sessionName>Learn QB in Minutes</event:sessionName> <br><event:sessionType>9</event:sessionType> <br><event:hostWebExID></event:hostWebExID> <br><event:startDate>02/12/2009</event:startDate> <br><event:endDate>02/12/2009</event:endDate> <br><event:timeZoneID>11</event:timeZoneID> <br><event:duration>30</event:duration> <br><event:description></event:description> <br><event:status>NOT_INPROGRESS</event:status> <br><event:panelists></event:panelists> <br><event:listStatus>PUBLIC</event:listStatus> <br></event:event> <br> <br>这种非标准的XML,没有定义命名空间,怎么办?在这种情况下,其实SimpleXmlElement就已经直接可以解决了,但是会报warnging,因为他认为event这个命名空间不存在。 <br>解决方法是: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>$xml = @new SimpleXmlElement($str);//在前面加@抑止错误。 <br>echo "<pre>"; <br>print_r($xml); <br> <br>目前看来,这种解决方法比较好。 <br><br>PHP SimpleXML 函数 相关资料<br>//www.gxlcms.com/w3school/php/php_ref_simplexml.htm<br>PHP SimpleXML<br>//www.gxlcms.com/w3school/php/php_xml_simplexml.htm</li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre>