0

When trying to parse an XML document in PHP, nothing is returned.

The XML document im trying to use:

http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml

The code I have tried:

$player = simplexml_load_file('http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml');
foreach ($player->PlayerName as $playerInfo) {
     echo $playerInfo['firstName'];
}

I have also tried:

$player = simplexml_load_file('http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml');
echo "Name: " . $player->PlayerName[0]['firstName'];

What do I need to change for the attributes to show?

3
  • It should be $xml->Player->PlayerName->firstName], since the xml goes CreationZone->Player->PlayerName. Commented Nov 27, 2014 at 15:50
  • PlayerName is not in the root. It's under the Player element. Have you tried ->Player->PlayerName[0]? Commented Nov 27, 2014 at 15:50
  • I tried $player->Player->PlayerName[0]->firstName; and $player->Player[0]->PlayerName[0]->firstName; still nothing. Commented Nov 27, 2014 at 15:52

3 Answers 3

1

You might try to print_r the whole data youself and finally find what you need:

var_dump($player->Player->PlayerName->Attrib['value']->__toString())
//⇒ string(7) "Daniele"
Sign up to request clarification or add additional context in comments.

Comments

1

To list all "values" (firstname, lastname,...) you need list all children and their attributes:

$xml = simplexml_load_file('http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml');

 foreach ($xml as $player) {
    foreach ($player->PlayerName->children() as $attrib) {
        echo $attrib['name'] . ': ' . $attrib['value'] . PHP_EOL; 
    }

 }

Output:

firstName: Daniele
lastName: Viola
commonName: Viola D.
commentaryName:

Comments

-1

This does not work, since you are trying to access an attribute and not a node value.

You might also run into problems, because the xml is not "valid" for simple xml. See my blogpost about the issues with parsing xml with php here http://dracoblue.net/dev/gotchas-when-parsing-xml-html-with-php/

If you use my Craur ( https://github.com/DracoBlue/Craur ) library instead, it will look like this:

$xml_string = file_get_contents('http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml');
$craur = Craur::createFromXml($xml_string);
echo $craur->get('Player.PlayerName.Attrib@value'); // works since the first attrib entry is the name

If you want to be sure about the attribute (or select another one) use:

$xml_string = file_get_contents('http://cdn.content.easports.com/media2011/fifa11zoneplayer/25068538/632A0001_10_ZONE_PLAYER_iUa.xml');
$craur = Craur::createFromXml($xml_string);
foreach ($craur->get('Player.PlayerName.Attrib[]') as $attribute)
{
    if ($attribute->get('@name') == 'firstName')
    {
        echo $attribute->get('@value');
    }
}

Comments

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.