Php 循环XML值
我导出了在google maps上创建的位置列表的.kml文件,如下所示:Php 循环XML值,php,xml,foreach,simplexml,Php,Xml,Foreach,Simplexml,我导出了在google maps上创建的位置列表的.kml文件,如下所示: <Placemark> <name>Name of first business here</name> <description><![CDATA[<div dir="ltr">Address etc <br>Tel No <br>www.example.com</div>]]></des
<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>
..
如果您想读取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;
?>
我把心留在了欧罗巴
游牧船
哦,木卫三
比法昌加
木卫四十四
杰维特·K·谢泼德
实际上不需要多个xml文件,glob函数与此无关,而且在foreach中缺少分号。。。无论如何,这不起作用,没有结果。没有运气,使用的代码:$distr=simplexml_load_文件('../../media/Distributors.kml');echo$distr->Placemark[0]->name代码>您的文件是否呈现?向我发送xml文件和php文件的详细信息。是的,文件加载非常好,正如问题中所说的,我确实设法提取了列表中单个企业的名称,只是它似乎没有使用相同的代码循环。我编写的代码。我无法从您发布的任何解决方案中获得任何输出。Charlie,为什么不回显$Disr的内容?echo var_dump($Disr);关于你的结果,我们至少会知道一两件事。使用了print\u r
和
,更好的可读性,在主要帖子中添加了,大多数样式的东西都是不相关的,所以我删除了一些由于字符数的原因。只是为了记录,我想告诉你,在主对象中有一个文档,文档中有一组值,我相信你应该在主对象的文档对象中搜索你的值。嗯,我以前尝试过,但没有运气。但是,找到了一个解决方案:$distr=simplexml_load_文件('../../media/Distributors.kml');对于($i=0;$i<23;$i++){foreach($distr as$value){echo'.$value->Placemark[$i]->name. ';}}}
@Charlie,解决方案中的for
循环应该只是一个内部foreach
,,查看每个$value
中的Placemark
元素列表:foreach($distr as$value){foreach($value->Placemark as$Placemark){echo'。$Placemark->name. ';}
<?php
$distr = simplexml_load_file('../../media/Distributors.kml');
foreach ($distr->Placemark as $placemark) {
echo '<li>' . $placemark->name . '</li>';
}
?>
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
)
)
)
)
)
<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;
?>