需要计算kml文件中有多少个子标记

时间:2015-06-04 18:38:01

标签: php arrays json xml kml

我有一些工作要转换kml文件并插入到mysql数据库中 我有点混淆计算我的kml文件下有多少子标记文件夹 这里有一点kml文件

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<name>FTTH 5 Cities 60K 20150409.kmz</name>
	<open>1</open>
	<Folder>
		<name>Kebun Jeruk, Jakarta</name>
		<Folder>
			<name>olt</name>
		</Folder>
		<Folder>
			<name>FEEDER</name>
		</Folder>
	</Folder>
	<Folder>
		<name>Kebun Jahe, Jakarta</name>
		<Folder>
			<name>olt</name>
		</Folder>
	</Folder>
</Document>
</kml>

这是另一个

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<name>FTTH 5 Cities 60K 20150409.kmz</name>
	<open>1</open>
	<Folder>
		<name>Kebun Jeruk, Jakarta</name>
		<Folder>
			<name>olt</name>
		</Folder>
		<Folder>
			<name>FEEDER</name>
		</Folder>
		<Folder>
			<name>CLUSTER FTTH KEBUN JERUK</name>
		</Folder>
	</Folder>
</Document>
</kml>

我现在要计算的代码是

$file = $_FILES['fileupload']['tmp_name'];
$xml_file = file_get_contents($file);
$xml = simplexml_load_string($xml_file);
$decodearray = json_decode(json_encode((array)$xml),1);

echo count($decodearray['Document']['Folder']);

需要计算文档 - &gt;文件夹(第一个)的子文件夹标记忽略其他来自两个kml文件的结果 2 来自另一个样本的 3

1 个答案:

答案 0 :(得分:0)

最后我找到了一个结果的小技巧

$file = 'file.kml';
$xml_file = file_get_contents($file);
$xml = simplexml_load_string($xml_file);
$decodearray = json_decode(json_encode((array)$xml),1);

$nowdatetime = date_create();
$tstamp = date_timestamp_get($nowdatetime);

switch (!isset($decodearray['Document']['Folder'][0])) {
  case true:
    $docfolder = $decodearray['Document']['Folder'];
    break;
  case false:
    $docfolder = $decodearray['Document']['Folder'][0];
    break;
}
echo count($docfolder['Folder']);

简单的切换案例到索引数组帮助很多..