从 SimpleXMLElement 对象获取值

发布于 2024-11-29 16:55:32 字数 832 浏览 0 评论 0原文

您好,我有以下 XML 片段:

......
            <Result number="4" position="1" points="25">
                <Driver driverId="button" url="http://en.wikipedia.org/wiki/Jenson_Button">
                    <GivenName>Jenson</GivenName>
                    <FamilyName>Button</FamilyName>
                    <DateOfBirth>1980-01-19</DateOfBirth>
                    <Nationality>British</Nationality>
                </Driver>
......

我可以轻松使用以下内容来获取 GiveName:

$item->Driver->GivenName;

但是当我使用:

$item->Driver->FamilyName;

我得到 SimpleXMLElement Object ()

我环顾四周,发现这可能与将其传递给字符串有关但后来我在屏幕上什么也没看到。甚至没有 SimpleXMLElement 对象。

我不明白,因为它是 GiveName 的兄弟姐妹并且有效。

Hi I have this following segment of XML:

......
            <Result number="4" position="1" points="25">
                <Driver driverId="button" url="http://en.wikipedia.org/wiki/Jenson_Button">
                    <GivenName>Jenson</GivenName>
                    <FamilyName>Button</FamilyName>
                    <DateOfBirth>1980-01-19</DateOfBirth>
                    <Nationality>British</Nationality>
                </Driver>
......

I can use the following easily to get the GivenName:

$item->Driver->GivenName;

But when I use:

$item->Driver->FamilyName;

I get SimpleXMLElement Object ()

I have looked around and found that it might be something to do with passing it to a string but then I get nothing on screen. Not even SimpleXMLElement Object.

I don't understand as it's a sibling of GivenName and that works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-12-06 16:55:32

在这两种情况下,您都会得到一个 SimpleXMLElement 对象,如果您使用 print_r(),您将看到该对象:

print_r ($item->Driver->GivenName);
print_r ($item->Driver->FamilyName);

输出:

SimpleXMLElement Object
(
    [0] => Jenson
)
SimpleXMLElement Object
(
    [0] => Button
)

您可以使用显式转换来获取字符串形式的值:

$givenNameString = (string) $item->Driver->GivenName;
$familyNameString = (string) $item->Driver->FamilyName;

You get a SimpleXMLElement object in both cases, which you'll see if you use print_r():

print_r ($item->Driver->GivenName);
print_r ($item->Driver->FamilyName);

Outputs:

SimpleXMLElement Object
(
    [0] => Jenson
)
SimpleXMLElement Object
(
    [0] => Button
)

You can use an explicit cast to get the values as strings:

$givenNameString = (string) $item->Driver->GivenName;
$familyNameString = (string) $item->Driver->FamilyName;
假装不在乎 2024-12-06 16:55:32

为了让 PHP 理解,你必须对对象进行强制类型转换,如下所示:

$givenName = (array) $item->Driver->GivenName; 
$familyName = (array) $item->Driver->FamilyName;

print_r($givenName);
print_r($familyName);

OUTPUT :

Array ([0] => 'Jenson')
Array ([0] => 'Button')

To make PHP understand you have to give typecasting forcefully on object like below:

$givenName = (array) $item->Driver->GivenName; 
$familyName = (array) $item->Driver->FamilyName;

print_r($givenName);
print_r($familyName);

OUTPUT :

Array ([0] => 'Jenson')
Array ([0] => 'Button')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文