从 SimpleXMLElement 对象中获取价值

编程入门 行业动态 更新时间:2024-10-26 08:32:32
本文介绍了从 SimpleXMLElement 对象中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下 XML 片段:

Hi I have this following segment of XML:

...... <Result number="4" position="1" points="25"> <Driver driverId="button" url="en.wikipedia/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 对象中获取价值

本文发布于:2023-11-26 22:11:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中   价值   SimpleXMLElement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!