XML 命名空间检索 foreach 循环中的子元素

XML Namespace retrieving sub-elements in foreach loop

提问人:Harry 提问时间:7/22/2023 更新时间:7/22/2023 访问量:26

问:

我有下面的XML代码。我正在使用命名空间来检索“api”值,该值在单独获取它们时可以成功运行,但是当我尝试检索同一 foreach 中的 2 个字段时,我只得到数组值。

<entry>
<id>tag:elements</id>
<category scheme="http://test.com" term="item" label="Item" />
<updated>2023-07-16T10:50:49.573+01:00</updated>
<link type="application/atom+xml" rel="alternate" href="https://test.com" />
<title>SOMEONE</title><content type="xhtml">
<api:object category="user" id="1234">
    <api:ever-approved>true</api:ever-approved>
    <api:privacy-level>Public</api:privacy-level>
    <api:privacy-level-locked>false</api:privacy-level-locked>
    <api:is-public>true</api:is-public>
    <api:allow-type-switching>true</api:allow-type-switching>
    <api:is-current-staff>true</api:is-current-staff>
    <api:is-login-allowed>true</api:is-login-allowed>
    <api:title>Ms</api:title>
    <api:initials>S</api:initials>
    <api:last-name>SOMEONE</api:last-name>
    <api:first-name>ME</api:first-name>
    <api:email-address>[email protected]</api:email-address>
    <api:primary-group-descriptor>DEPARTMENT</api:primary-group-descriptor>
    <api:user-identifier-associations user-id="1234">
    <api:user-identifier-association scheme="email-address" status="claimed" decision="always-me">[email protected]</api:user-identifier-association>
</api:object>
</entry>

这适用于单独检索字段:

$xml = simplexml_load_file('mypeople.xml', NULL, NULL, 'http://www.w3.org/2005/Atom');
$xml->registerXPathNamespace('api', 'http://www.test.com/publications/api');
foreach($xml->xpath('//api:object/api:email-address') as $header)
{
echo $header;
}

foreach($xml->xpath('//api:object/api:user-identifier-associations/@user-id') as $xd_item)
{
echo $xd_item;

}

但是当我在循环中尝试如下内容时,它失败了(我尝试将命名空间重新注册到 $entry但这没有帮助。

foreach ($xml->entry as $entry) {
$entry->registerXPathNamespace('api', 'http://www.test.com/publications/api');
echo($entry->xpath('//api:object/api:email-address'));
echo($entry->xpath('//api:object/api:user-identifier-associations/@user-id'));
}
php xml 解析

评论

0赞 Nigel Ren 7/22/2023
调用将始终返回一个数组。xpath

答:

0赞 James 7/22/2023 #1

为什么不为您需要的每个值设置变量呢?

$xml = simplexml_load_file('mypeople.xml', NULL, NULL, 'http://www.w3.org/2005/Atom');
$xml->registerXPathNamespace('api', 'http://www.test.com/publications/api');

foreach ($xml->entry as $entry) {
    $entry->registerXPathNamespace('api', 'http://www.test.com/publications/api');

    // Grab the values
    $emailAddress = (string)$entry->xpath('//api:object/api:email-address')[0];
    $userId = (string)$entry->xpath('//api:object/api:user-identifier-associations/@user-id')[0];

    // Now you can use them as needed
    echo "Email Address: " . $emailAddress . "\n";
    echo "User ID: " . $userId . "\n";
}

评论

0赞 Harry 7/24/2023
嗨,詹姆斯,非常感谢。它关闭了,但由于某种原因,它只显示第一条记录。它返回正确数量的记录,但它们都是相同的。
0赞 Harry 7/24/2023
所以只需要添加一个计数器..
0赞 Harry 7/24/2023
$counter = 0;foreach ($xml->xpath('//api:object') as $entry) { $emailAddress = (string)$entry->xpath('//api:object/api:email-address')[$counter]; $userId = (string)$entry->xpath('//api:object/api:user-identifier-associations/@user-id')[$counter]; // 现在您可以根据需要使用它们 echo “Email Address: ” .$emailAddress .“\n”;echo “用户 ID: ” 。$userId .“<br>”;$counter++;}