问题描述
我有以下 XML 片段:
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>
......
我可以轻松地使用以下内容来获取 GivenName:
I can use the following easily to get the GivenName:
$item->Driver->GivenName;
但是当我使用:
$item->Driver->FamilyName;
我得到 SimpleXMLElement 对象 ()
I get SimpleXMLElement Object ()
我环顾四周,发现这可能与将它传递给字符串有关,但随后我在屏幕上什么也没有.甚至不是 SimpleXMLElement 对象.
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.
我不明白,因为它是 GivenName 的兄弟姐妹并且有效.
I don't understand as it's a sibling of GivenName and that works.
推荐答案
在这两种情况下你都会得到一个 SimpleXMLElement 对象,如果你使用 print_r() 就会看到:
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);
输出:
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;
这篇关于从 SimpleXMLElement 对象中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!