Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from SimpleXMLElement Object

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.

like image 582
Cool Hand Luke Avatar asked Aug 30 '26 16:08

Cool Hand Luke


1 Answers

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;
like image 169
RussSchick Avatar answered Sep 02 '26 13:09

RussSchick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!