XML/PHP-从多个文档节点提取信息
我想知道是否有人能帮我写一些代码 我有一个KML(Google Earth XML),我正试图将它导入到我的应用程序中 我遇到的问题是,在提取数据时,它一直工作到第一个节点,并且不会处理文件的其余部分 KML文件非常庞大,因此我将在下面展示一个示例:XML/PHP-从多个文档节点提取信息,php,xml,Php,Xml,我想知道是否有人能帮我写一些代码 我有一个KML(Google Earth XML),我正试图将它导入到我的应用程序中 我遇到的问题是,在提取数据时,它一直工作到第一个节点,并且不会处理文件的其余部分 KML文件非常庞大,因此我将在下面展示一个示例: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/
<?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">
<Folder>
<name>Kalamunda</name>
<open>1</open>
<Document>
<name>Export 1418786594.kml</name>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.072723865509,-31.98064436184923,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0731288790703,-31.98067621355563,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>76-122 Tourist Drive 207</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0735687613487,-31.98064436184923,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>12 Hinkler Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0740676522255,-31.98083319680418,0</coordinates>
</Point>
</Placemark>
</Document>
<Document>
<name>Export 1418785221.kml</name>
<Placemark>
<name>LOT 435 Collins Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0624188184738,-31.97887202447751,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>25 Central Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0589158535004,-31.9744125903374,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>49 Canning Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0574942827225,-31.97629422494352,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>13 Anne Ave</name>
<description>Walliston WA 6076</description>
<Point>
<coordinates>116.0673272609711,-31.99400063250859,0</coordinates>
</Point>
</Placemark>
</Document>
<Document>
<name>Export 1418783501.kml</name>
<Placemark>
<name>3 Lookout Rd</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.037417948246,-31.98043960061557,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>9 Ozone Terrace</name>
<description>Kalamunda WA 6076</description>
<Point>
<coordinates>116.0382279753685,-31.98028261669366,0</coordinates>
</Point>
</Placemark>
</Document>
</Folder>
</kml>
使用这段代码,它将提取数据直到12 Hinkler Rd placemark,我认为因为有一个结束标记,它不会再进一步
如果给定一个这种格式的XML,我将如何提取所有数据
任何帮助都将不胜感激
干杯,所有
节点最容易通过XPath查询获得。由于位于kml
命名空间下,因此可以查询//kml:Placemark
。/
将在XML中的任何级别搜索表达式Placemark
我们可以通过巧妙地结合您已经拥有的explode()
来大大简化您最初拥有的循环的内部工作。嵌套explode()
在array_slice()
中,检索前两个坐标分量并丢弃第三个(,0
)可以在一个操作中完成,并立即将它们分配给数组键['coordinates']
此外,您可以直接将一个新的子数组粘贴到$data
集合数组上,而不是维护递增的$i
// Query all <Placemark> elements at once
$placemarks = $xml_data->xpath('//kml:Placemark');
// Loop over them and construct an array of child properties
$data = array();
foreach ($placemarks as $mark) {
// Collect the properties into a new array and append
// that to $data with []
$data[] = array(
// Cast SimpleXML elements to strings with (string)
'name' => (string)$mark->name,
'description' => (string)$mark->description,
// Get the coords in one action - explode on the ','
// but return only the first two elements via array_slice()
'coordinates' => array_slice(explode(',', $mark->Point->coordinates), 0, 2)
);
}
print_r($data);
// Displays:
Array
(
[0] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.072723865509
[1] => -31.98064436184923
)
)
[1] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0731288790703
[1] => -31.98067621355563
)
)
[2] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0735687613487
[1] => -31.98064436184923
)
)
... SNIP....
... SNIP....
... SNIP....
[8] => Array
(
[name] => 3 Lookout Rd
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.037417948246
[1] => -31.98043960061557
)
)
[9] => Array
(
[name] => 9 Ozone Terrace
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0382279753685
[1] => -31.98028261669366
)
)
)
//一次查询所有元素
$placemarks=$xml_data->xpath('//kml:Placemark');
//循环它们并构造一个子属性数组
$data=array();
foreach($placemarks作为$mark){
//将属性收集到新数组中并追加
//使用[]将其转换为$data
$data[]=数组(
//将SimpleXML元素强制转换为具有(字符串)的字符串
'name'=>(字符串)$mark->name,
'description'=>(字符串)$mark->description,
//在一个动作中获得坐标-在“,”上爆炸
//但仅通过数组_slice()返回前两个元素
“坐标”=>array_切片(分解(“,”,$mark->Point->coordinates),0,2)
);
}
打印(数据);
//显示:
排列
(
[0]=>阵列
(
[名称]=>76-122旅游大道207
[说明]=>卡拉姆达WA 6076
[坐标]=>数组
(
[0] => 116.072723865509
[1] => -31.98064436184923
)
)
[1] =>阵列
(
[名称]=>76-122旅游大道207
[说明]=>卡拉姆达WA 6076
[坐标]=>数组
(
[0] => 116.0731288790703
[1] => -31.98067621355563
)
)
[2] =>阵列
(
[名称]=>76-122旅游大道207
[说明]=>卡拉姆达WA 6076
[坐标]=>数组
(
[0] => 116.0735687613487
[1] => -31.98064436184923
)
)
剪
剪
剪
[8] =>阵列
(
[名称]=>Lookout路3号
[说明]=>卡拉姆达WA 6076
[坐标]=>数组
(
[0] => 116.037417948246
[1] => -31.98043960061557
)
)
[9] =>阵列
(
[名称]=>9臭氧露台
[说明]=>卡拉姆达WA 6076
[坐标]=>数组
(
[0] => 116.0382279753685
[1] => -31.98028261669366
)
)
)
这是您感兴趣的地名-名称、描述、坐标?是@MichaelBerkowskiHey,谢谢。。我收到一个错误,它说unexpected[在有$data=[]的行上;您不能使用PHP5.4。请使用$data=array()。
代替。同样,在循环中,您需要$data[]=array(…)
代替$data[]=[…]
Darn-这可能是我第一次在回答中使用5.4+数组语法,希望安装基数最终足够高:-(@BigJobbies)我刚刚编辑过,将其转换回旧式的array()
声明。我想有一天……很高兴能提供帮助。祝你好运。
// Query all <Placemark> elements at once
$placemarks = $xml_data->xpath('//kml:Placemark');
// Loop over them and construct an array of child properties
$data = array();
foreach ($placemarks as $mark) {
// Collect the properties into a new array and append
// that to $data with []
$data[] = array(
// Cast SimpleXML elements to strings with (string)
'name' => (string)$mark->name,
'description' => (string)$mark->description,
// Get the coords in one action - explode on the ','
// but return only the first two elements via array_slice()
'coordinates' => array_slice(explode(',', $mark->Point->coordinates), 0, 2)
);
}
print_r($data);
// Displays:
Array
(
[0] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.072723865509
[1] => -31.98064436184923
)
)
[1] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0731288790703
[1] => -31.98067621355563
)
)
[2] => Array
(
[name] => 76-122 Tourist Drive 207
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0735687613487
[1] => -31.98064436184923
)
)
... SNIP....
... SNIP....
... SNIP....
[8] => Array
(
[name] => 3 Lookout Rd
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.037417948246
[1] => -31.98043960061557
)
)
[9] => Array
(
[name] => 9 Ozone Terrace
[description] => Kalamunda WA 6076
[coordinates] => Array
(
[0] => 116.0382279753685
[1] => -31.98028261669366
)
)
)