需要统计kml文件中有多少子标签
Need to count how many child tag in kml files
我有一些工作要转换 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']);
需要计算 Document->Folder(1st) 的子 Folder 标签,忽略其他
结果我从两个 kml 文件都是 2 应该是 3 来自另一个样本
我终于找到了一个小技巧来做结果
$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']);
索引数组的简单切换案例有很大帮助..
我有一些工作要转换 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']);
需要计算 Document->Folder(1st) 的子 Folder 标签,忽略其他
结果我从两个 kml 文件都是 2 应该是 3 来自另一个样本
我终于找到了一个小技巧来做结果
$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']);
索引数组的简单切换案例有很大帮助..