开发者

parsing a xml to get some values

http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_heade开发者_开发百科r=0

These are player sales from a browser game. I want to save some fields from these sales. I am fetching that xml with curl and storing on my server. Then do the following:

$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/transfer');

print_r($items);

foreach($items as $item) {
    echo $item['buyerTeamname'], ': ', $item['sellerTeamname'], "\n";
}

The array is empty and i cant seem to get anything from it. What am i doing wrong?


There is no reason to use cURL or XPath for that. You can do

$url = 'http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0';
$transfers = new SimpleXMLElement($url, NULL, TRUE);
foreach($transfers->transfer as $transfer) {
    printf(
        "%s transfered from %s to %s\n",
        $transfer->playerName,
        $transfer->sellerTeamname,
        $transfer->buyerTeamname
    );
}

Live Demo


You forgot a slash in your xpath:

$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('/*/transfer');

print_r($items);

foreach($items as $item) {
    echo $item->buyerTeamname, ': ', $item->sellerTeamname, "\n";
}


<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
?>

Is this what you want?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜