0

I get this result from overpass api - this is streets.

<?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>

So i need to parse it and return every street, but when i trying to parse it with http://www.php.net/manual/en/book.dom.php i cant get anything by id. My code is below:

$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));
    }
}
Kin
  • 4,466
  • 13
  • 54
  • 106
  • 1
    [_“For this function to work, you will need …”_](http://www.php.net/manual/en/domdocument.getelementbyid.php) – CBroe Dec 03 '13 at 13:03
  • 2
    What is the expected output? Instead of `getElementById`, I'd just use an XPath expression. Something like: `$xpath = new DOMXPath($doc); $elem = $xpath->query("//*[@id='$id']")->item(0);` – Amal Murali Dec 03 '13 at 13:26
  • @CBroe, thanks, now i see the problem... May be you have some ideas how to add `DTD` to the existing `DOMDocument` object? I i googleld, only thing about `openstreetmap` is http://wiki.openstreetmap.org/wiki/API_v0.6/DTD – Kin Dec 03 '13 at 13:29
  • Follow up on @AmalMuralis hint. – CBroe Dec 03 '13 at 13:33
  • @CBroe yeah, a lot of hardcode will occur... I thing i better use `simplexml_load_string` and once loop trough all xml. – Kin Dec 03 '13 at 13:41
  • What does this have to do with “hard-coding”? If you have the id value you are looking for in a variable, then you insert that into the XPath expression dynamically … – CBroe Dec 03 '13 at 13:47
  • Depending on the desired output format, it might be easier to process the XML with XSL, making use of `xsl:key`. – Alf Eaton Dec 04 '13 at 12:13

1 Answers1

-1

The code below should loop through the nodes which is what it sounds like you're trying to do. The code you have is looping through the tags and the tags under each of those which do not contain any IDs.

$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);
}

Hope this helps at least get you on the right track.

Brandon
  • 229
  • 1
  • 4