CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
SimpleXMLElement::xpath - how to sort a osm-request output



Sep 13, 2015, 1:29 AM

Post #1 of 2 (1082 views)
SimpleXMLElement::xpath - how to sort a osm-request output Can't Post

hello dear community,

i know little php and a very very little perl.... but i am no expert here... - unfortunatley... so this question needs your advice:

I tried to extract some data from an .Osm File. -.-you can run a demo-command here at overpass turbo i want to store the data in a database - (or at least in an excel-file)

So I have - within an amount of xml-data - something like this:

"tags": {
"addr:city": "Barcelona",
"addr:housenumber": "1",
"addr:postcode": "08022",
"addr:street": "Carrer de Torras i Pujalt",
"amenity": "hospital",
"contact:email": "",
"contact:fax": "+34 932124050",
"contact:phone": "+34 932122300",
"contact:website": "",
"name": "Cliníca Sagrada Família"

So, the question is this: how can store all those values I tried to make some kind of foreach without sucess. And definitely I don't know why is suposse we have one Iteration that get all the values and why is not possible to store together.

Well i heard about that we can use SimpleXMLElement::xpath here to get all the values
and the corresponding values. For example :

$raw = <<<EOF
"type": "node",
"id": 583257940,
"lat": 41.4134446,
"lon": 2.1426945,
"tags": {
"amenity": "hospital",
"emergency": "yes",
"name": "Clinica Delfos"
"type": "node",
"id": 618312181,
"lat": 41.4138593,
"lon": 2.1970778,
"tags": {
"addr:city": "Barcelona",
"addr:housenumber": "211",
"addr:postcode": "08020",
"addr:street": "Carrer de Fluvià",
"amenity": "hospital",
"health_facility:type": "health_centre",
"medical_system:western": "yes",
"name": "CAP Sant Martí",
"phone": "+93 307 07 66"
"type": "node",
"id": 876348001,
"lat": 41.3841883,
"lon": 2.1952253,
"tags": {
"amenity": "hospital",
"emergency": "yes",
"name": "Hospital del Mar",
"website": "",
"wheelchair": "yes"

$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>";

question: how to use SimpleXMLElement::xpath here?

btw - you can run a demo-command here at overpass turbo

This has been generated by the overpass-turbo wizard.
The original search was:
“hospital in rome”
// fetch area “” to search in
// gather results
// query part for:rome “hospital”
// print results
out body;
out skel qt;

then you get lots of results....
question: how to store those results in either excel or mysql?

i really love to hear from you

many thanks in advance

Veteran / Moderator

Sep 13, 2015, 2:10 AM

Post #2 of 2 (1081 views)
Re: [dilbert] SimpleXMLElement::xpath - how to sort a osm-request output [In reply to] Can't Post

Your input does not look like XML, but more like JSON. Maybe you want to try a JSON module from CPAN, or possibly the JSON::PP module which has been a core module since Perl 5.14.

Otherwise, I am not very clear, from what you said, about what exactly you want to store into your database or your spreadsheet. In most cases, though, the first step would be to transform your input data into a, internal Perl data structure (probably a hash of hashes, or some variation thereof) in order to manipulate your data conveniently.

Your data might end up as a HoH similar to this:

$VAR1 = { 
'lat' => '41.4138593',
'lon' => '2.1970778',
'id' => 618312181,
'type' => 'node',
'tags' => {
'health_facility:type' => 'health_centre',
'addr:housenumber' => '211',
'name' => 'CAP Sant Martí',
'addr:postcode' => '08020',
'phone' => '+93 307 07 66',
'medical_system:western' => 'yes',
'amenity' => 'hospital',
'addr:city' => 'Barcelona',
'addr:street' => 'Carrer de Fluvià'

which is then quite easy to rework for inserting into a DB or a spreadsheet. (Or you might want to further process the addr:xxx tags into a further nested structure, it depends what you really need.)

(This post was edited by Laurent_R on Sep 13, 2015, 2:12 AM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives