将XML导入Sql数据库

时间:2015-01-10 11:31:51

标签: php xml database

我有这个xml:

 -<lfm status="ok">
-<track>
<id>602366984</id>
<name>Barbarism Begins At Home (BBC TV Tube performance)</name>
<mbid/>
-<url>http://www.last.fm/music/The+Smiths/_/Barbarism+Begins+At+Home+(BBC+TV+Tube+performance)</url>
<duration>384000</duration>
<streamable fulltrack="0">0</streamable>
<listeners>10</listeners>
<playcount>24</playcount>
-<artist>
<name>The Smiths</name>
<mbid>40f5d9e4-2de7-4f2d-ad41-e31a9a9fea27</mbid>
<url>http://www.last.fm/music/The+Smiths</url>
</artist>
<toptags>
</toptags>
</track>
</lfm>

我想获取曲目和艺术家的名字,我应该使用2个代码(下一个代码)还是有更好的方法?

foreach ($xml -> track as $row) {
            $title = $row -> name;
            $artist = $row -> name;
            $duration = $row -> duration;

在这个代码中,我得到了两个艺术家的名字。

1 个答案:

答案 0 :(得分:1)

foreach ($xml -> track as $row) {
            $title = $row -> name;
            $artist = $row ->artist-> name;
            $duration = $row -> duration;