如何使用PHP读取XML API

时间:2015-12-20 09:22:50

标签: php arrays xml

我正在学习PHP,需要知道如何将以下XML API中的元素放入Array中。我尝试使用simplexml_load_file()函数。但它给了我一个错误。

这是我的代码。

BluetoothGattService mService = new BluetoothGattService(mServiceUuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic mCharacteristic1 = new BluetoothGattCharacteristic(mChar1Uuid, BluetoothGattCharacteristic.PROPERTY_WRITE, 0);
    BluetoothGattCharacteristic mCharacteristic2 = new BluetoothGattCharacteristic(mChar2Uuid, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0);
    BluetoothGattDescriptor mChar2Cccd = new BluetoothGattDescriptor(mCccdUuid, 0);

2 个答案:

答案 0 :(得分:0)

请使用以下代码。

$xml=simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");
$channel_info = $xml->channel;
$totalRecords = count($channel_info->item);
for ($i=0; $i < $totalRecords; $i++) { 
    echo $channel_info->item[$i]->title . '<br/>';
}

我希望它对你有用。

谢谢。

答案 1 :(得分:0)

如果你这样做:

$xml=simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");
echo '<pre>';
print_r($xml);
echo '</pre>';

你得到一个更加人性化的结构:

  

SimpleXMLElement对象(       [@attributes] =&gt;排列           (               [版本] =&gt; 2.0           )

[channel] => SimpleXMLElement Object
    (
        [title] => Cricinfo Live Scores
        [ttl] => 2
        [link] => http://www.cricinfo.com
        [description] => Latest scores from Cricinfo
        [copyright] => (c)Cricinfo
        [language] => en-gb
        [pubDate] => Sun, 20 Dec 2015 09:55:01 +0000
        [item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [title] => Lions 214/10 &  316/8  v Dolphins 137/2 &  141/10 *
                        [link] => http://www.cricinfo.com/ci/engine/match/851997.html?CMP=OTC-RSS 
                        [description] => Lions 214/10 &  316/8  v Dolphins 137/2 &  141/10 *
                        [guid] => http://www.cricinfo.com/ci/engine/match/851997.html
                    )

                [1] => SimpleXMLElement Object
                    (
                        [title] => Titans 369/10 &  171/10  v Warriors 204/5 &  209/10 *
                        [link] => http://www.cricinfo.com/ci/engine/match/852039.html?CMP=OTC-RSS 
                        [description] => Titans 369/10 &  171/10  v Warriors 204/5 &  209/10 *
                        [guid] => http://www.cricinfo.com/ci/engine/match/852039.html
                    )

                [2] => SimpleXMLElement Object
                    (
                        [title] => New Zealand 142/5 &  237/10 * v Sri Lanka 292/10 &  133/10 
                        [link] => http://www.cricinfo.com/ci/engine/match/914205.html?CMP=OTC-RSS 
                        [description] => New Zealand 142/5 &  237/10 * v Sri Lanka 292/10 &  133/10 
                        [guid] => http://www.cricinfo.com/ci/engine/match/914205.html
                    )
     

等...

因此,您可以在$ match中看到所需的信息: 通道 - &GT;项目

所以:

foreach($xml->channel->item as $match) {
    print_r($match);
    echo '<hr><br>';
}

将从xml网址中概述匹配项。 然后,您可以使用这样的子元素,例如:

foreach($xml->channel->item as $match) {
    echo $match->title;
    echo '<hr><br>';
}