繁体   English   中英

无法使用PhP SimpleXMLElement解析XML

[英]Trouble parsing XML using PhP SimpleXMLElement

我正在使用Google的配置API,我正在使用PhP的SimpleXmlElement来解析XML响应。

SimpleXmlElement未正确解析响应。 这是一个样本。

<?php

$xml_response = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
  <id>https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com</id>
  <updated>2014-05-06T00:53:35.817Z</updated>
  <link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com'/>
  <link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com'/>
  <apps:property name='userEmail' value='liz@example.com' />
  <apps:property name='aliasEmail' value='helpdesk@gethelp_example.com' />
</entry>
EOD;
$xml = new SimpleXmlElement($xml_response);
print_r($xml);

?>

输出:

SimpleXMLElement Object
(
  [id] => https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com
  [updated] => 2014-05-06T00:53:35.817Z
  [link] => Array
    (
      [0] => SimpleXMLElement Object
        (
          [@attributes] => Array
            (
              [rel] => self
              [type] => application/atom+xml
              [href] => https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com
            )
          )
      [1] => SimpleXMLElement Object
        (
          [@attributes] => Array
            (
              [rel] => edit
              [type] => application/atom+xml
              [href] => https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com
            )
        )
    )
)

SimpleXmlElement只删除两个<apps:property …\\>条目。 我正在使用PhP 5.3.28。 是否有其他选项/参数可以与SimpleXmlElement一起使用以使其工作?

有关Google配置API的详细信息, 访问https://developers.google.com/google-apps/provisioning/

提示给MackieeERaf以及使用SimpleXML对命名空间的Parse XML进行评论,这使我得到了适当标题的博客文章@ http://blog.preinheimer.com/index.php?/archives/172-SimpleXML, -Namespaces-吹风loss.html

这就是我必须做的事情:

<?php
$xml_response = <<<EOD
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
  <id>https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com</id>
  <updated>2014-05-06T00:53:35.817Z</updated>
  <link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com'/>
  <link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/alias/2.0/gethelp_example.com/helpdesk%40gethelp%5Fexample.com'/>
  <apps:property name='userEmail' value='liz@example.com' />
  <apps:property name='aliasEmail' value='helpdesk@gethelp_example.com' />
</entry>
EOD;

$xml = new SimpleXmlElement($xml_response);
$alias_email = $xml->children('apps', true)->property[0]->attributes();
$user_email = $xml->children('apps', true)->property[1]->attributes();

$alias_email['value']; // gives you the aliasEmail value
$user_email['value']; // gives you the userEmail value
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM