<?php
$xml = "<Placemark>
<name>ED1YBY</name>
<description><![CDATA[145.750 MHz -600 kHz FM (RV60 - R6)<br>IN62IG<br>URE Ourense]]></description>
<styleUrl>#icon-503-DB4436</styleUrl>
<Point>
<coordinates>
-7.281189,42.271212,0
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>ED1YBY</name>
<description><![CDATA[145.750 MHz -600 kHz FM (RV60 - R6)<br>IN62IG<br>URE Ourense]]></description>
<styleUrl>#icon-503-DB4436</styleUrl>
<Point>
<coordinates>
-7.281189,42.271212,0
</coordinates>
</Point>
</Placemark>
";
$obj = new SimpleXMLExtractor($xml);
$name_array = array();
$description_array = array();
$coordinates_array = array();
foreach ($obj->name as $value1) {
$name_array[] = trim((string)$value1);
}
foreach ($obj->description as $value2) {
$description_array[] = trim((string)$value2);
}
foreach ($obj->Point->coordinates as $value3) {
$coordinates_array = trim((string)$value3);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>EXTRACTING DATA</title>
</head>
<body>
<center>
<table>
<tr>
<th><h4 style = "width:300px; text-align:center">FREQUENCY</h4></th>
<th><h4 style = "width:140px; text-align:center">CALLSIGN</h4></th>
<th><h4 style = "width:140px; text-align:center">LOCATOR</h4></th>
<th><h4 style = "width:140px; text-align:center">LAT</h4></th>
<th><h4 style = "width:140px; text-align:center">LONG</h4></th>
<th><h4 style = "width:140px; text-align:center">SySop</h4></th>
</tr>
<?php
for ($i = 0; $i < count($name_array); $i++) {
echo "<tr style = 'text-align:center'>";
$notes = explode("<br>", $description_array[$i]);
echo "<td>".$notes[0]."</td>";
echo "<td>".$name_array[$i]."</td>";
echo "<td>".$notes[1]."</td>";
$latlon = explode("," , $coordinates_array);
echo "<td>".$latlon[1]."</td>";
echo "<td>".$latlon[0]."</td>";
echo "<td>".$notes[2]."</td>";
echo "</tr>";
}
?>
</table>
</center>
</body>
</html>
我第一次使用SimpleXMLExtractor。我写下了这样的代码,如果$ xml变量中只有一个Placemark元素,它可以很好地工作。 如果我在$ xml中添加了第二个Placemark元素(实际上是130个元素,说实话!)错误会显示出来。 我阅读了有关SimpleXMLExtractor的文档,但无法查看并找到一个好的解决方案。任何帮助将不胜感激!
我已经更新了插入第二个地方标记的代码,而不是整个130组。当存在多于1个地标元素时,此类代码会崩溃。
答案 0 :(得分:0)
我在php.net上快速搜索了SimpleXMLExtractor。它没有发现任何结果。
请替换此行:
$obj = new SimpleXMLExtractor($xml);
有了这个:
$obj = simplexml_load_string($xml);
另一个问题是您需要一个根XML标记。例如,
<placemarkers>
<placemarker></placemarker>
<placemarker></placemerker>
</placemarkers>
此外,将您的XML复制并粘贴到此http://www.xmlvalidation.com/网站以验证您的XML。