热门在DomDocument中通过id获取节点?

时间:2013-12-03 12:57:22

标签: php domdocument overpass-api

我从overpass api得到了这个结果 - 这就是街道。

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2013-12-03T12:52:02Z"/>

  <node id="1549294055" lat="49.4310141" lon="7.5117213"/>
  <node id="1549294085" lat="49.4313484" lon="7.5126816"/>
  <node id="1549294087" lat="49.4315384" lon="7.5132431"/>
  <node id="1549294093" lat="49.4318250" lon="7.5140125"/>
  <node id="1549294094" lat="49.4318541" lon="7.5140969"/>
  <node id="1549294104" lat="49.4322262" lon="7.5151568"/>
  <node id="1549294106" lat="49.4324901" lon="7.5159332"/>
  <node id="1552775307" lat="49.4328287" lon="7.5169585"/>
  <node id="1552775309" lat="49.4328551" lon="7.5170364"/>
  <node id="1552775318" lat="49.4330332" lon="7.5176039"/>
  <node id="1552775347" lat="49.4333308" lon="7.5186515"/>
  <node id="1552775375" lat="49.4341515" lon="7.5215118"/>
  <node id="1552775408" lat="49.4345873" lon="7.5229784"/>
  <node id="1552775447" lat="49.4358841" lon="7.5273364"/>
  <node id="1552775464" lat="49.4367267" lon="7.5302234"/>
  <node id="1552809430" lat="49.4368016" lon="7.5304614"/>
  <way id="28367045">
    <nd ref="1549294106"/>
    <nd ref="1552775307"/>
    <nd ref="1552775309"/>
    <nd ref="1552775318"/>
    <nd ref="1552775347"/>
    <nd ref="1552775375"/>
    <nd ref="1552775408"/>
    <nd ref="1552775447"/>
    <nd ref="1552775464"/>
    <nd ref="1552809430"/>
    <tag k="highway" v="secondary"/>
    <tag k="ref" v="L 356"/>
  </way>
  <way id="141545567">
    <nd ref="1549294104"/>
    <nd ref="1549294106"/>
    <tag k="bridge" v="yes"/>
    <tag k="highway" v="secondary"/>
    <tag k="layer" v="1"/>
    <tag k="ref" v="L 356"/>
  </way>
  <way id="141545568">
    <nd ref="1549294055"/>
    <nd ref="1549294085"/>
    <nd ref="1549294087"/>
    <nd ref="1549294093"/>
    <nd ref="1549294094"/>
    <nd ref="1549294104"/>
    <tag k="highway" v="secondary"/>
    <tag k="ref" v="L 356"/>
  </way>

</osm>

所以我需要解析它并返回每条街道,但是当我尝试用http://www.php.net/manual/en/book.dom.php解析它时,我无法通过id获取任何内容。我的代码如下:

$doc = new DOMDocument;
$doc->loadXML($result);

$ways = $doc->getElementsByTagName('way');

foreach ($ways as $way) {
    $nodes = $way->getElementsByTagName('nd');

    foreach ($nodes as $node) {
        $id = intval($node->getAttribute('ref'));

        var_dump($id);
        var_dump($doc->getElementById($id));
    }
}

1 个答案:

答案 0 :(得分:-1)

下面的代码应该遍历节点,这听起来就像你正在尝试做的那样。您拥有的代码循环遍历标签和每个不包含任何ID的标签。

$doc = new DOMDocument;
$doc->loadXML($result);

$nodes = $doc->getElementsByTagName('node');

foreach ($nodes as $node) {
    $id = intval($node->getAttribute('id'));
    $lat = intval($node->getAttribute('lat'));
    $lon = intval($node->getAttribute('lon'));

    var_dump($node);
}

希望这有助于至少让你走上正轨。