循环XML值

时间:2013-09-26 09:03:28

标签: php xml foreach simplexml

我导出了.kml文件,用于在Google地图上创建的地方列表,如下所示:

 <Placemark>
    <name>Name of first business here</name>
    <description><![CDATA[<div dir="ltr">Address etc
<br>Tel No
<br>www.example.com</div>]]></description>
    <styleUrl>#style17</styleUrl>
    <Point>
      <coordinates>1893891893891</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>Second business</name>
    <description><![CDATA[<div dir="ltr">Address here 
<br>Tel no
<br>www.webaddress.com</div>]]></description>
    <styleUrl>#style5</styleUrl>
    <Point>
      <coordinates>7317783871871</coordinates>
    </Point>
  </Placemark>
<Placemark>
..
</Placemark>
..

我只想循环并将名称回显到页面。

我尝试将文件作为simplexml文件加载并使用foreach循环尝试读取值,但没有成功。


此代码仅打印出第一个名称,似乎不循环,在第一个名称后停止:

<?php
    $distr = simplexml_load_file('../../media/Distributors.kml');
    foreach ($distr as $value) {
        echo '<li>' . $value->Placemark->name . '</li>';
    }
?>

此代码看起来应该可以正常工作,但不打印任何内容:

<?php
    $distr = simplexml_load_file('../../media/Distributors.kml');
    foreach ($distr->Placemark as $placemark) {
        echo '<li>' . $placemark->name . '</li>';
    }
?>

我尝试过其他一些组合,但似乎都没有做我想做的事,我做错了什么?

Print_r dump:

SimpleXMLElement Object
(
    [Document] => SimpleXMLElement Object
        (
            [name] => Distributors
            [description] => SimpleXMLElement Object
                (
                )

            [Style] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style17
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
                                        )

                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style5
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
                                        )

                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style4
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style18
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
                                        )

                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style15
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style20
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [6] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style6
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [7] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style23
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [8] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style22
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [9] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style11
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [10] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style7
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [11] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style12
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [12] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style3
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                    [22] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => style8
                                )

                            [IconStyle] => SimpleXMLElement Object
                                (
                                    [Icon] => SimpleXMLElement Object
                                        (
                                            [href] => SimpleXMLElement Object
                                                (
                                                )

                                        )

                                )

                        )

                )

            [Placemark] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [name] => Rigibore Ltd
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style17
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -5.399673,50.191875,0.000000
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [name] => Rigibore Inc.
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style5
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -88.320183,42.867550,0.000000
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [name] => Vibha Technologies
                            [styleUrl] => #style4
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 77.568222,13.002831,0.000000
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [name] => Centra-Tech bvba
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style18
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 4.867222,51.141304,0.000000
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [name] => A B T Machine Tools & Tooling Ltd
                            [styleUrl] => #style15
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -4.324394,55.894394,0.000000
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [name] => Cromwell Tools Ltd WDC
                            [styleUrl] => #style20
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -1.124264,52.585289,0.000000
                                )

                        )

                    [6] => SimpleXMLElement Object
                        (
                            [name] => Beijing Promise
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style6
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 116.300972,39.945293,0.000000
                                )

                        )

                    [7] => SimpleXMLElement Object
                        (
                            [name] => Donini E Grandi Srl Do-Gra
                            [styleUrl] => #style23
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 11.331189,44.512886,0.000000
                                )

                        )

                    [8] => SimpleXMLElement Object
                        (
                            [name] => Euskron S.A.
                            [styleUrl] => #style22
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -1.947317,43.093163,0.000000
                                )

                        )

                    [9] => SimpleXMLElement Object
                        (
                            [name] => H B Tools Ltd
                            [styleUrl] => #style11
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -2.865015,53.601593,0.000000
                                )

                        )

                    [10] => SimpleXMLElement Object
                        (
                            [name] => Monks & Crane Ltd
                            [styleUrl] => #style7
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -2.028771,52.547211,0.000000
                                )

                        )

                    [11] => SimpleXMLElement Object
                        (
                            [name] => Hudson Foster
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style12
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -1.509982,53.665184,0.000000
                                )

                        )

                    [12] => SimpleXMLElement Object
                        (
                            [name] => BIM Sp. z o.o.
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style3
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 16.772060,52.437359,0.000000
                                )

                        )

                    [13] => SimpleXMLElement Object
                        (
                            [name] => Precise Tooling System
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [styleUrl] => #style9
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 103.875557,1.377493,0.000000
                                )

                        )

                    [14] => SimpleXMLElement Object
                        (
                            [name] => Campbell Peter Sales Ltd
                            [styleUrl] => #style2
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -3.949751,55.848644,0.000000
                                )

                        )

                    [15] => SimpleXMLElement Object
                        (
                            [name] => Obradors D R M, S.l.
                            [styleUrl] => #style16
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 2.171534,41.400570,0.000000
                                )

                        )

                    [16] => SimpleXMLElement Object
                        (
                            [name] => Multicarb CC T/A Multitrade Distributors
                            [styleUrl] => #style1
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 28.178808,-26.152639,0.000000
                                )

                        )

                    [17] => SimpleXMLElement Object
                        (
                            [name] => Howard Lee & Son
                            [styleUrl] => #style10
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -2.062812,52.471966,0.000000
                                )

                        )

                    [18] => SimpleXMLElement Object
                        (
                            [name] => Glendower Cutting Tools Ltd
                            [styleUrl] => #style21
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -1.105845,52.674934,0.000000
                                )

                        )

                    [19] => SimpleXMLElement Object
                        (
                            [name] => Finn A Værktøj A/S
                            [styleUrl] => #style13
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 9.853188,54.986698,0.000000
                                )

                        )

                    [20] => SimpleXMLElement Object
                        (
                            [name] => Flatley.PMG
                            [styleUrl] => #style14
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -6.337717,53.312447,0.000000
                                )

                        )

                    [21] => SimpleXMLElement Object
                        (
                            [name] => Iscar Tools Argentina Sa
                            [styleUrl] => #style19
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => -58.404968,-34.639957,0.000000
                                )

                        )

                    [22] => SimpleXMLElement Object
                        (
                            [name] => Duroc Machine Tool AB
                            [styleUrl] => #style8
                            [Point] => SimpleXMLElement Object
                                (
                                    [coordinates] => 18.092628,59.449158,0.000000
                                )

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

如果你想读取xml文件并循环遍历xml元素。可以像这样

<songs>
    <song dateplayed="2011-07-24 19:40:26">
        <title>I left my heart on Europa</title>
        <artist>Ship of Nomads</artist>
    </song>
    <song dateplayed="2011-07-24 19:27:42">
        <title>Oh Ganymede</title>
        <artist>Beefachanga</artist>
    </song>
    <song dateplayed="2011-07-24 19:23:50">
        <title>Kallichore</title>
        <artist>Jewitt K. Sheppard</artist>
    </song>
</songs>
<?php

$dir = "home/";
$files = glob($dir."*.xml");
foreach($files as $file_xml)
{
$headernode = new SimpleXMLElement($file_xml,null,true);
foreach($headernode as $property)
{
echo $name = $property->first;
}

}

OR try something like this

 $mysongs = simplexml_load_file('songs.xml');
  echo $mysongs->song[0]->artist;


?>