我尝试从.Osm文件中提取一些数据。
所以我有这样的事情:
<way id="28747493" visible="true" version="7" changeset="9347177" timestamp="2011-09-19T21:48:11Z" user="Camilo Alvarez" uid="492132">
<nd ref="316077528"/>
<nd ref="316077503"/>
<tag k="highway" v="primary"/>
<tag k="lanes" v="1"/>
<tag k="name" v="Calle 51"/>
<tag k="oneway" v="yes"/>
<tag k="ref" v="Boyacá"/>
</way>
<way id="28747492" visible="true" version="9" changeset="7227086" timestamp="2011-02-08T15:33:22Z" user="dmartinh" uid="314700">
<nd ref="358031212"/>
<nd ref="316077505"/>
<tag k="foot" v="permissive"/>
<tag k="highway" v="footway"/>
<tag k="name" v="Calle 52"/>
</way>
所以,我想提取每一个&#34; ref&#34;并放入一个名为&#34; referencia的表格,我想分配标签名称&#34; calle 51&#34;那个参考值。
这样的事情:
表参考
idnode -------- via
316077528 | Calle 51 |
316077503 | Calle 51 |
358031212 | Calle 52 |
316077505 | Calle 52 |
----------------------
所以,我可以存储&#34; idnode&#34;价值得很好,但我无法存储&#34; via&#34;值。我试图在没有成功的情况下制造某种形式的foreach。
我绝对不知道为什么我们有一个迭代得到&#34; idnode&#34;价值和&#34;通过&#34;为什么不可能一起存放。
<?php < ? global $referencia;
global $via;
/*
/**
* OSM Overpass API with PHP SimpleXML / XPath
*
* PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets)
*/
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('map');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$xml = simplexml_load_file("map.osm");
$counter = - 1;
foreach($xml->children() AS $child)
{
$counter++;
if ($child->getName() == 'way')
{
// if($child->getName() == 'node'){
// echo $counter;
$name = "";
$id = "";
$lat = "";
$lon = "";
$name = $child['version'];
$id = $child['id'];
$lat = $child['lat'];
$lon = $child['lon'];
}
foreach($child->children() AS $grandchild)
{
if ($grandchild->getname() == 'nd')
{
$referencia = $grandchild['ref'];
if ($grandchild->getName() == 'tag')
{
if ($grandchild['k'] == 'name')
{
$via = $grandchild['v'];
}
}
mysql_query("INSERT INTO referencia (idnode,via) VALUES ('$referencia','$via')");
}
}
}
?>
先谢谢。
答案 0 :(得分:1)
您可以使用SimpleXMLElement::xpath同时获取&#34;来自&#34;和相应的&#34; idnode&#34;值。例如:
$raw = <<<EOF
<root>
<way id="28747493" visible="true" version="7" changeset="9347177" timestamp="2011-09-19T21:48:11Z" user="Camilo Alvarez" uid="492132">
<nd ref="316077528"/>
<nd ref="316077503"/>
<tag k="highway" v="primary"/>
<tag k="lanes" v="1"/>
<tag k="name" v="Calle 51"/>
<tag k="oneway" v="yes"/>
<tag k="ref" v="Boyacá"/>
</way>
<way id="28747492" visible="true" version="9" changeset="7227086" timestamp="2011-02-08T15:33:22Z" user="dmartinh" uid="314700">
<nd ref="358031212"/>
<nd ref="316077505"/>
<tag k="foot" v="permissive"/>
<tag k="highway" v="footway"/>
<tag k="name" v="Calle 52"/>
</way>
</root>
EOF;
$xml = simplexml_load_string($raw);
foreach($xml->xpath("//way") AS $way){
$via = $way->xpath("tag[@k='name']/@v")[0];
foreach($way->nd AS $nd){
$idnode = $nd["ref"];
echo $idnode .", ". $via ."<br>";
}
}
<强> Demo 强>
输出
316077528, Calle 51
316077503, Calle 51
358031212, Calle 52
316077505, Calle 52
xpath解释:
//way
选择XML文档中任何位置的所有<way>
元素。
tag[@k='name']
选择当前上下文节点的<tag>
子节点,k
属性值等于name
。然后,从<tag>
,/@v
返回v
属性。