【问题标题】:Looping XML Values循环 XML 值
【发布时间】:2013-09-26 09:03:28
【问题描述】:

我为在谷歌地图上创建的地点列表导出了 .kml 文件,如下所示:

 <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 转储:

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
                                )

                        )

                )

        )

)

【问题讨论】:

  • 查理,你为什么不回显一下$distr 的内容呢?回声 var_dump($distr);至少我们会知道你的结果的一两件事。
  • 使用print_r&lt;pre&gt;,更好的可读性,在主帖中添加,大部分样式内容无关紧要,所以我删除了一些由于字符数。
  • 只是为了记录,我想告诉你,在主对象里面有一个Document,在Document里面有一组值,我相信你应该在Document里面搜索你的值您的主要对象的对象。
  • 嗯,我之前确实尝试过,但没有运气。不过,找到了解决办法:$distr = simplexml_load_file('../../media/Distributors.kml'); for($i=0; $i &lt; 23; $i++) { foreach ($distr as $value) { echo '&lt;li&gt;' . $value-&gt;Placemark[$i]-&gt;name . '&lt;/li&gt;'; } }
  • @Charlie 解决方案中的for 循环应该只是一个内部foreach,以查看每个$value 中的Placemark 元素列表:foreach ($distr as $value) { foreach ($value-&gt;Placemark as $placemark) { echo '&lt;li&gt;' . $placemark-&gt;name . '&lt;/li&gt;'; } }

标签: php xml foreach simplexml


【解决方案1】:

如果你想读取 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;


?>

【讨论】:

  • 真的不需要多个 xml 文件,glob 函数无关紧要,而且你在 foreach 中缺少一个分号......无论如何,这不起作用,没有结果。
  • 不走运,使用的代码:$distr = simplexml_load_file('../../media/Distributors.kml'); echo $distr-&gt;Placemark[0]-&gt;name;
  • 您的文件是否呈现?向我发送您的 xml 文件和 php 文件所在的文件详细信息。
  • 是的,文件加载非常好,正如问题中所说,我确实设法提取了列表中单个企业的名称,只是它似乎没有使用相同的代码循环。
  • 我写的那个。我无法从您发布的任何解决方案中获得任何输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多