【发布时间】:2012-09-05 16:52:27
【问题描述】:
我目前正在尝试从 google 联系人 api 中提取信息。使用库中提供的示例代码:
$response = json_encode(simplexml_load_string($val->getResponseBody()));
$result = json_decode($response, true);
simplexml_load_string 函数不使用命名空间。看php.net怎么加参数我有点迷茫。
$xml = json_encode(simplexml_load_string(null, $val->getResponseBody(), null, 'gd', true));
试图从我的 xml 字符串中提取“gd”命名空间的事件我收到一个错误。有人可以帮忙吗?
输出错误是这样的:
simplexml_load_string() 期望参数 2 是从 SimpleXMLElement 派生的类名
但除此错误之外,它还正确打印了我想要的信息...我很困惑。
我需要提取的信息示例:
<gd:extendedProperty xmlns:gs='http://schemas.google_apps_sync.com' name='google_apps_sync'>
<gs:em_odn1>awalker@xxxxx.com</gs:em_odn1>
<gs:em_dn1>First Name Last Name</gs:em_dn1>
<gs:em_ad1>awalker@xxxxxxxxx.com</gs:em_ad1>
<gs:em_t1>SMTP</gs:em_t1>
<gs:f_c>32791</gs:f_c>
<gs:f>Last, First</gs:f>
</gd:extendedProperty>
编辑
$xmlResponse = simplexml_load_string($val->getResponseBody(), null, 0, 'gd', true);
$xmlResponse->registerXPATHNamespace('gd', 'http://schemas.google_apps_sync.com');
$xmlResponse->registerXPATHNamespace('gs', 'http://schemas.google_apps_sync.com/contact/2008');
上面的代码允许我访问我需要的命名空间,但一次只能访问一个......例如:
$email = json_encode($xmlResponse->xpath('//gd:email'), true));
$postal = json_encode($xmlResponse->xpath('//gd:postalAddress'), true);
$name = json_encode($xmlResponse->xpath('//gs:em_dn1'), true);
如果我打印这些变量,我会得到我需要的相应信息,但是在我启动了我想要的命名空间之后,我是否无法访问原始的 $xmlResponse 变量?
例如:
json_decode($xmlResponse, true);
不会给我带有我定义的命名空间的 xml 信息。
【问题讨论】: