欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php simplexmlElement操作xml的命名空间实现代码

程序员文章站 2022-10-25 19:44:18
看了这个问题,第一个反应就是namespace的关系,但我从来没有使用simplexml操作过namespace,于是就翻开手册查了一下资料,问题并没有解决,最终是通过go...
看了这个问题,第一个反应就是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>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' 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>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/*' 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);

目前看来,这种解决方法比较好。

php simplexml 函数 相关资料
php simplexml