You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP如何读取XML文档中<a:int>标签的节点值

XML带命名空间节点解析失败解决方案

问题原因

<a:int>节点使用了独立的自定义命名空间http://schemas.microsoft.com/2003/10/Serialization/Arrays,SimpleXMLElement默认仅解析当前层级的默认命名空间节点,不会主动读取带自定义前缀的命名空间内容,因此直接通过对象属性获取会返回空。

实现代码

// 加载API返回的XML字符串
$xml = simplexml_load_string($your_xml_response_string);

// 定位到departmentKeys节点
$departmentKeys = $xml->JobSet->Job->departmentKeys;

// 方法1:直接指定命名空间URI读取子节点
$a_namespace = 'http://schemas.microsoft.com/2003/10/Serialization/Arrays';
$a_nodes = $departmentKeys->children($a_namespace);
$dept_id = (int)$a_nodes->int;
// 此时$dept_id的值即为1608

// 方法2:先获取所有命名空间映射,通过前缀读取,适配命名空间可能变动的场景
$all_namespaces = $xml->getNamespaces(true);
$a_nodes = $departmentKeys->children($all_namespaces['a']);
$dept_id = (int)$a_nodes->int;

多值场景适配

如果departmentKeys下存在多个<a:int>节点,直接遍历即可:

foreach ($a_nodes->int as $value) {
    echo (int)$value . PHP_EOL;
}

内容的提问来源于stack exchange,提问作者b.ferdinand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 15:48:03