1

I'm using the simplexml_load_file command to parse data from an api to the MMO I subscribe to make a tool to get the current guild rooster. The raw xml that is returned from the api looks like:

<apiresponse>
    <guild name="Seekers of Forgotten Pasts" world="Cannith" memberCount="1312">
        <characters>
            <character name="Nethershadow" surname="" rank_level="5" rank_name="Officer" race="Halfling" gender="m" alignment="True Neutral">
            <classes>
                <class name="Ranger" level="2"/>
                <class name="Rogue" level="8"/>
            </classes>
        </character>
        <character name="..." surname="..." rank_level="..." rank_name="..." race="..." gender="..." alignment="...">
            ...
        </character>
        <character name="Calomel" surname="" rank_level="10" rank_name="Member" race="Drow Elf" gender="m" alignment="Lawful Good">
            <classes>
                <class name="Paladin" level="5"/>
            </classes>
        </character>
    </guild>
<cache_info cached_until_gmt="2013-01-05 19:23"/>
</apiresponse>

Now, when I plug in my php to retrieve the list of names, as such:

<?php
    $names = array();
    $xmlGuild = simplexml_load_file($DataDDO_api_urlFullGuildSearch);
    $characters = $xmlGuild->guild->characters->character;
    for ($i = 0; $i < sizeof($characters); $i++) {
        $names[] = $characters[$i]->attributes()->name;
    }
    echo "\n\nTotal members: " . count($names) . "\n\n<pre>\n";
    print_r($names);
    echo "</pre>\n";
?>

I get as a result:

Total members: 1312

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => Nethershadow
        )
    [...] => SimpleXMLElement Object
        (
            [0] => ...
        )
    [1311] => SimpleXMLElement Object
        (
            [0] => Calomel
        )
)

Instead of what I am expecting of:

Array
(
    [0] => Nethershadow
    [...] => ...
    [1311] => Calomel
)

Why is this?

2
  • 1
    As an aside, could it be that you mean count() instead of sizeof()? Also, this is better solved with a foreach loop. Commented Jan 5, 2013 at 16:16
  • I tried the loop section as foreach ($characters as $character) {$names[] = $character->attributes()->name;} and got the same results. Commented Jan 5, 2013 at 16:22

1 Answer 1

1

You must cast SimpleXML objects to string if you want their text value.

<?php
    $names = array();
    $apiresponse = simplexml_load_file($DataDDO_api_urlFullGuildSearch);

    foreach ($apiresponse->guild->characters->character as $char) 
    {
        $names[] = (string)$char->attributes()->name;
    }

    echo "\n\nTotal members: " . count($names) . "\n\n<pre>\n";
    print_r($names);
    echo "</pre>\n";
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect... so it is the (string) in $names[] = (string)$char->attributes()->name; that makes it work, just to be clear for other future instances?
@Shoe Yes. $char->attributes()->name returns a SimpleXMLElement object instance (see docs), not a string. You must make an explicit cast to string; this is the standard way to get the text value. Otherwise the whole object reference is stored in the $names array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.