Parse XML response with SimpleXML

Einfach Dinge, die nichts mit XAMPP, Apache Friends, Apache, MySQL, PHP und alle dem zu tun haben. Allerlei halt. ;)

Parse XML response with SimpleXML

Postby unleash_it » 13. June 2014 16:10

Hi,

I need help with retrieving some data from a XML file.
I make a request to an URL and I want to parse the response XML file to get lat and long values.
What I did was the following:

Code: Select all
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);


$data variable contains the following:


SimpleXMLElement Object
(
[@attributes] => Array
(
[timestamp] => Sat, 15 Jun 13 20:02:13 +0000
[attribution] => Data Š OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright]OpenStreetMap
[querystring] => Bucharest-Romania
[polygon] => false
[exclude_place_ids] => 331526
[more_url] => http://nominatim.openstreetmap.org/search?format=xml&exclude_place_ids=331526&q=Bucharest-Romania
)

[place] => SimpleXMLElement Object
(
[@attributes] => Array
(
[place_id] => 331526
[osm_type] => node
[osm_id] => 96209423
[place_rank] => 15
[boundingbox] => 44.4361381530762,44.4361419677734,26.1027431488037,26.1027450561523
[lat] => 44.436139
[lon] => 26.1027436
[display_name] => București, Sector 2, Bucuresti, România, European Union
[class] => place
[type] => city
[importance] => 0.73231672860554
[icon] => http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png
)

)

)

I tried the following code to get the lat and long values but with no success:

Code: Select all
$lat = $data->place[0]->lat;
$lon = $data->place[0]->lon;



Any suggestions?

Thank you,
Interessen: Bikes & steel frames: Linux & SBC https://www.allaboutcircuits.com :: die neuen Knowledge-Base: AFFiNE: There can be more than Notion and Miro. auf affine.pro :: WordPress Entwicklung - sic: make.wordpress.org/core/
User avatar
unleash_it
 
Posts: 753
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Re: Parse XML response with SimpleXML

Postby JJ_Tagy » 14. June 2014 17:52

Not sure if you will be able to do one complex line for it. Try this as an example:
Code: Select all
<?php
  $xml = file_get_contents("http://nominatim.openstreetmap.org/search?format=xml&exclude_place_ids=331526&q=Bucharest-Romania");
  $data = new SimpleXMLElement($xml);
  echo "<b>SimpleXML Array:</b><br/>";
  print_r($data);
  echo "<br/><br/>";
  echo "<b>Place 0:</b><br/>";
  print_r($data->place[0]);
  echo "<br/><br/>";
  $place0 = $data->place[0];
  echo "Lat: " . $place0['lat'] . "<br/>";
  echo "Lon: " . $place0['lon'] . "<br/><br/>";
  echo "<b>All Places:</b><br/>";
  foreach ($data->place as $place) {
    echo "Place ID: " . $place['place_id'] . "<br/>";
    echo "OSM Type: " . $place['osm_type'] . "<br/>";
    echo "OSM ID: " . $place['osm_id'] . "<br/>";
    echo "Place Rank: " . $place['place_rank'] . "<br/>";
    echo "Bounding Box: " . $place['boundingbox'] . "<br/>";
    echo "Lattitude: " . $place['lat'] . "<br/>";
    echo "Longitude: " . $place['lon'] . "<br/>";
    echo "Display Name: " . $place['display_name'] . "<br/>";
    echo "Class: " . $place['class'] . "<br/>";
    echo "Type: " . $place['type'] . "<br/>";
    echo "Importance: " . $place['importance'] . "<br/>";
    echo "Icon: " . $place['icon'] . "<br/>";
    echo "<br/>";
  }
?>
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Parse XML response with SimpleXML

Postby unleash » 19. June 2014 11:21

hello dear jj - many many thanks for the hints - i will try out this


AGAIN - thanks!!
have a great day

greetings
Last edited by unleash on 19. June 2014 12:38, edited 1 time in total.
unleash
 
Posts: 147
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1

Re: Parse XML response with SimpleXML

Postby unleash » 19. June 2014 12:38

hello dear buddy - i have another question -

sure you can help me here tooo...

I am new to PHP's SimpleXML. Below I have added my own answer to the question, how to refine the code to ad more tags.

I want to filter the data to get the nodes with special category. Here is sample of the OSM data I want to get the whole schools within an area. The first script runs well - but now I want to refine the search and add more tags. I want to store all into MySQL.

So we need to make some XML parsing with PHP:

The following is a little OSM Overpass API example with PHP SimpleXM

we need to do this here

Code: Select all
<?php
/**
 * 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)
 */


//
// 1.) Query an OSM Overpass API Endpoint
//

$query = 'node
  ["amenity"~".*"]
  (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';

$context = stream_context_create(['http' => [
    'method'  => 'POST',
    'header' => ['Content-Type: application/x-www-form-urlencoded'],
    'content' => 'data=' . urlencode($query),
]]);

# please do not stress this service, this example is for demonstration purposes only.
$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);
$start = microtime(true);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

?>


i just have had a quick view on the above mentioned site. i try to figure out how to do this - for any and all hints i am thankful


The following code lists all schools and tries to obtain their names as well. I have not covered translations yet because my sample data didn't have those, but you can also look for all kind of names including translations and just prefer a specific one):

Code:

//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}


The key point here are the xpath queries.

In the above mentioend example two are used, the first xpath queriy is to get the nodes that have certain tags.I think this is the most interesting one for me:


Code: Select all
//node[tag[@k = "amenity" and @v = "school"]]


This line says: Give us all node elements that have a tag element inside which has the k attribute value "amenity" and the v attribute value "school". This is the condition we have to filter out those nodes that are tagged with amenity school.

Further on xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:


Code: Select all
tag[@k = "name"]/@v'


This line says: Relative to the current node, give me the v attribute from a tag element that as the k attribute value "name". As you can see, some parts are again similar to the line before. I think you can both adopt them to your needs.

Because not all school nodes have a name, a default string is provided for display purposes by adding it to the (then empty) result array:


Code: Select all
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
                                                    ^^^^^^^^^^^^^^^
                                                Provide Default Value



So here my results for that code-example:
Query returned xxxxx node(s) and took 1.10735 seconds.
2179 School(s) found: the last ones are shown below.....

Code: Select all
#2151: ID:2688357765 [51.4668941,-0.9731135] New Directrions, North Reading
#2152: ID:2702504696 [51.5884265,-0.7829013] Burford School
#2153: ID:2702549737 [51.5802201,-0.7653918] Great Marlow School
#2154: ID:2706219304 [51.3779317,-0.0895302] ARK Oval Primary Academy
#2155: ID:2706219314 [51.3871935,-0.0623001] Ashburton Primary School
#2156: ID:2706219320 [51.3210977,-0.1398859] CALAT Smitham Centre
#2157: ID:2706219326 [51.3638861,-0.0922032] Elmhurst School
#2158: ID:2706219339 [51.4007121,-0.0743710] Harris Academy South Norwood
#2159: ID:2706219343 [51.3831662,-0.0405476] Orchard Way Primary School
#2160: ID:2706219347 [51.3531047,-0.0959447] Purley Oaks Primary School
#2161: ID:2706219348 [51.3428384,-0.0069931] Rowdown Primary School
#2162: ID:2706219350 [51.3954917,-0.0732185] South Norwood Primary School
#2163: ID:2706219351 [51.3377151,-0.1230482] St David's Preparatory School
#2164: ID:2706219353 [51.3993760,-0.1144352] Winterbourne School
#2165: ID:2717394621 [51.8706538,0.1480886] Prep
#2166: ID:2717394636 [51.8685838,0.1463720] Pre-Prep
#2167: ID:2722704201 [51.1398429,-0.0457445] Felbridge Primary School
#2168: ID:2723815070 [50.8465429,-0.3030261] Lancing College
#2169: ID:2727170814 [51.5780664,-0.0249051] Adult Education Centre
#2170: ID:2833253896 [50.9928140,-0.7774996] (unnamed)
#2171: ID:2837001831 [51.1783749,-0.7970866] More House School
#2172: ID:2865091022 [50.9090614,-0.5565425] Dorset House School
#2173: ID:2882477853 [51.6261198,-0.7349665] Bowerdean Primary School
#2174: ID:2901434856 [51.6542477,-0.3098923] The Haberdashers' Aske's Girls School
#2175: ID:2901434857 [51.6565707,-0.3129822] The Haberdashers' Aske's Boys School


and now i try to figure out how i can enter more xpath queries at the above mentioned code


and get out even more important data - see here Key:contact - OpenStreetMap Wiki

Code: Select all
contacthone
contact:fax
contact:website
contact:email

i will digg into all documents and come back later the weekend... and report all the findings


well - i think that i need to extend the xpath requests within the loop where xpath is used again,
now relative to those school nodes to see if there is a name and if so to fetch it:


Code: Select all
tag[@k = "name"]/@v'
tag[@k = "    contact:website"]/@v'
tag[@k = "    contact:email"]/@v'



hope you can help me...

look forward to hear from you
unleash
 
Posts: 147
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1

Re: Parse XML response with SimpleXML

Postby JJ_Tagy » 21. June 2014 20:49

I'm not sure exactly what you are asking so I looked at the given code and what was available from that initial query and noticed only extra data could be the postcode(zipcode). I added that field as an example if that helps.

Code: Select all
<?php
/**
 * 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)
 */


//
// 1.) Query an OSM Overpass API Endpoint
//

$query = 'node
  ["amenity"~".*"]
  (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';

$context = stream_context_create(['http' => [
    'method'  => 'POST',
    'header' => ['Content-Type: application/x-www-form-urlencoded'],
    'content' => 'data=' . urlencode($query),
]]);

# please do not stress this service, this example is for demonstration purposes only.
$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);
$start = microtime(true);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
echo "<br/>";
foreach ($schools as $index => $school)
{
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    list($zip) = $school->xpath('tag[@k = "addr:postcode"]/@v');
    printf("#%02d: ID:%' -10s  [%s,%s]  %s  ZIP:%05d", $index, $school['id'], $school['lat'], $school['lon'], $name, $zip);
    echo "<br/>";
}
?>
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Parse XML response with SimpleXML

Postby unleash » 27. June 2014 00:32

hello dear jj tagy

many many thanks - you helped me alot.

your answer saved my day !!!!!!!!!!!!!!!!!!!!! ;-)

have a great day.


update: see the output.....



Query returned 1693 node(s) and took 2.45366 seconds.

23 School(s) found:
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#00: ID:332534486 [39.5017565,16.2721899] Scuola Primaria ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#01: ID:1428094278 [39.3320912,16.1862820] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#02: ID:1822746784 [38.9075566,16.5776597] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#03: ID:1822755951 [38.9120272,16.5713431] (unnamed) ZIP:00000<br/>#04: ID:1903859699 [38.6830409,16.5522243] Liceo Scientifico Statale A. Guarasci ZIP:88068<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#05: ID:2002566438 [39.1349460,16.0736446] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#06: ID:2056891127 [39.4106679,16.8254844] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#07: ID:2056892999 [39.4124687,16.8286119] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#08: ID:2272010226 [39.4481717,16.2894353] SCUOLA DELL'INFANZIA SAN FRANCESCO ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#09: ID:2272017152 [39.4502366,16.2807664] Scuola Media ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#10: ID:2358307794 [39.5015031,16.3905965] I.I.S.S. Liceo Statale V. Iulia ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#11: ID:2358307796 [39.4926280,16.3853662] Liceo Classico ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#12: ID:2358307797 [39.4973761,16.3858275] Scuola Media ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#13: ID:2358307800 [39.5015527,16.3941156] I.T.C. e per Geometri ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#14: ID:2358307801 [39.4983862,16.3807796] Istituto Professionale ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#15: ID:2358344885 [39.4854617,16.3832419] Istituto Tecnico Statale Commerciale G. Falcone ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#16: ID:2448031004 [38.6438417,16.3873106] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#17: ID:2458139204 [39.0803263,17.1291649] Sacro Cuore ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#18: ID:2552412313 [39.0765212,17.1224610] (unnamed) ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#19: ID:2582443083 [39.0815417,17.1178983] Liceo Socio Biologico Gravina ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#20: ID:2585754364 [38.8878393,16.4076323] Scuola Elementare ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#21: ID:2585754366 [38.8877600,16.4076216] Scuola Media ZIP:00000<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm30.php on line 48
#22: ID:2755576944 [39.3641541,17.1310900] (unnamed) ZIP:00000<br/>
martin@linux-70ce:~/php> ^C
martin@linux-70ce:~/php>



with that script

Code: Select all



    <?php
    /**
     * 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)
     */


    //
    // 1.) Query an OSM Overpass API Endpoint
    //

    $query = 'node
      ["amenity"~".*"]
      (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
    out;';

    $context = stream_context_create(['http' => [
        'method'  => 'POST',
        'header' => ['Content-Type: application/x-www-form-urlencoded'],
        'content' => 'data=' . urlencode($query),
    ]]);

    # please do not stress this service, this example is for demonstration purposes only.
    $endpoint = 'http://overpass-api.de/api/interpreter';
    libxml_set_streams_context($context);
    $start = microtime(true);

    $result = simplexml_load_file($endpoint);
    printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

    //
    // 2.) Work with the XML Result
    //

    # get all school nodes with xpath
    $xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
    $schools = $result->xpath($xpath);
    printf("%d School(s) found:\n", count($schools));
    echo "<br/>";
    foreach ($schools as $index => $school)
    {
        # Get the name of the school (if any), again with xpath
        list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
        list($zip) = $school->xpath('tag[@k = "addr:postcode"]/@v');
        printf("#%02d: ID:%' -10s  [%s,%s]  %s  ZIP:%05d", $index, $school['id'], $school['lat'], $school['lon'], $name, $zip);
        echo "<br/>";
    }
    ?>




i will try out more things and try out to investigate how you did the trick and then i will
go to extend the script... and to do some cleanups....


again - many many thanks!!

love to hear from you

greetings

update:
by the way - have a closer look at this page: https://wiki.openstreetmap.org/wiki/Tag ... y%3Dschool

well i guess that we can have a look for some more tags . what do you tink -
i will digg d eeper into the code.


greetings

perhaps we can itterate trohgh the list of tags with a foreach loop.... what do you say....?
Code: Select all
//tag[@k="amenity" and @v="school"]/..


love to hear from you
unleash
 
Posts: 147
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1

Re: Parse XML response with SimpleXML

Postby JJ_Tagy » 27. June 2014 03:04

In php.ini, you can suppress warnings and notices.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Parse XML response with SimpleXML

Postby unleash_it » 27. June 2014 14:27

hi there - by the way

can we extend the xpath -requests a bit.
i will try to do that at the weekend.

i will do it as you - extendint the xpath-requests the same way...

i come back later the weekend and let you know...


(see above the link to the tags - there som more. I will try out what i can do!

greetings
Interessen: Bikes & steel frames: Linux & SBC https://www.allaboutcircuits.com :: die neuen Knowledge-Base: AFFiNE: There can be more than Notion and Miro. auf affine.pro :: WordPress Entwicklung - sic: make.wordpress.org/core/
User avatar
unleash_it
 
Posts: 753
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Re: Parse XML response with SimpleXML

Postby unleash_it » 27. June 2014 16:36

hello good day


want to extend the serach / retrieval and the xpath-requests with the following dataset - the tagset-

cf http://wiki.openstreetmap.org/wiki/Tag:amenity%3Dschool


operator=* - Name of operator, often the local education authority.
addr=* - Address details
capacity=*, for the number of pupils taught at the school
isced:level=*, for the educational level (proposed tags)
fee=yes if the the school makes a direct charge for core services.
religion=*, if the school is associated with a particular religion (also denomination=*)
wikipedia=*, for a link to a Wikipedia article about the school
website=*, for a link to the school's own website




http://wiki.openstreetmap.org/wiki/Tag:amenity%3Dschool

i hope that i will make some efforts over the weekend .
note i come back and report all the findings
Interessen: Bikes & steel frames: Linux & SBC https://www.allaboutcircuits.com :: die neuen Knowledge-Base: AFFiNE: There can be more than Notion and Miro. auf affine.pro :: WordPress Entwicklung - sic: make.wordpress.org/core/
User avatar
unleash_it
 
Posts: 753
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Re: Parse XML response with SimpleXML

Postby unleash » 27. June 2014 17:57

hello i tried this

with the request that llooks for websites


Code: Select all


        <?php
        /**
         * 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)
         */


        //
        // 1.) Query an OSM Overpass API Endpoint
        //

        $query = 'node
          ["amenity"~".*"]
          (38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
        out;';

        $context = stream_context_create(['http' => [
            'method'  => 'POST',
            'header' => ['Content-Type: application/x-www-form-urlencoded'],
            'content' => 'data=' . urlencode($query),
        ]]);

        # please do not stress this service, this example is for demonstration purposes only.
        $endpoint = 'http://overpass-api.de/api/interpreter';
        libxml_set_streams_context($context);
        $start = microtime(true);

        $result = simplexml_load_file($endpoint);
        printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

        //
        // 2.) Work with the XML Result
        //

        # get all school nodes with xpath
        $xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
        $schools = $result->xpath($xpath);
        printf("%d School(s) found:\n", count($schools));
        echo "<br/>";
        foreach ($schools as $index => $school)
        {
            # Get the name of the school (if any), again with xpath
            list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
            list($zip) = $school->xpath('tag[@k = "addr:postcode"]/@v');
            list($website) = $school->xpath('tag[@k = "addr:website"]/@v');
           
            printf("#%02d: ID:%' -10s  [%s,%s]  %s  ZIP:%05d", $index, $school['id'], $school['lat'], $school['lon'], $name, $zip, $website [%s,%s]);
            echo "<br/>";
        }
        ?>




i got back the folllowing results...
Code: Select all
martin@linux-70ce:~/php> php osm100.php
PHP Parse error:  syntax error, unexpected '%', expecting ']' in /home/martin/php/osm100.php on line 54
martin@linux-70ce:~/php>


i dunno wahat happenshere
unleash
 
Posts: 147
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1

Re: Parse XML response with SimpleXML

Postby unleash » 27. June 2014 18:24

Code: Select all
        // 2.) Work with the XML Result
        //

        # get all school nodes with xpath
        $xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
        $schools = $result->xpath($xpath);
        printf("%d School(s) found:\n", count($schools));
        echo "<br/>";
        foreach ($schools as $index => $school)
        {
            # Get the name of the school (if any), again with xpath
            list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
            list($zip) = $school->xpath('tag[@k = "addr:postcode"]/@v');
            list($website) = $school->xpath('tag[@k = "addr:website"]/@v');
           
            printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name, $zip, $website );
            echo "<br/>";
        }
        ?>



Query returned 1694 node(s) and took 2.26477 seconds.

23 School(s) found:
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#00: ID:332534486 [39.5017565,16.2721899] Scuola Primaria
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#01: ID:1428094278 [39.3320912,16.1862820] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#02: ID:1822746784 [38.9075566,16.5776597] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#03: ID:1822755951 [38.9120272,16.5713431] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#04: ID:1903859699 [38.6830409,16.5522243] Liceo Scientifico Statale A. Guarasci
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#05: ID:2002566438 [39.1349460,16.0736446] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#06: ID:2056891127 [39.4106679,16.8254844] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#07: ID:2056892999 [39.4124687,16.8286119] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#08: ID:2272010226 [39.4481717,16.2894353] SCUOLA DELL'INFANZIA SAN FRANCESCO
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#09: ID:2272017152 [39.4502366,16.2807664] Scuola Media
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#10: ID:2358307794 [39.5015031,16.3905965] I.I.S.S. Liceo Statale V. Iulia
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#11: ID:2358307796 [39.4926280,16.3853662] Liceo Classico
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#12: ID:2358307797 [39.4973761,16.3858275] Scuola Media
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#13: ID:2358307800 [39.5015527,16.3941156] I.T.C. e per Geometri
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#14: ID:2358307801 [39.4983862,16.3807796] Istituto Professionale
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#15: ID:2358344885 [39.4854617,16.3832419] Istituto Tecnico Statale Commerciale G. Falcone
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#16: ID:2448031004 [38.6438417,16.3873106] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#17: ID:2458139204 [39.0803263,17.1291649] Sacro Cuore
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#18: ID:2552412313 [39.0765212,17.1224610] (unnamed)
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#19: ID:2582443083 [39.0815417,17.1178983] Liceo Socio Biologico Gravina
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#20: ID:2585754364 [38.8878393,16.4076323] Scuola Elementare
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#21: ID:2585754366 [38.8877600,16.4076216] Scuola Media
<br/>PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 51
PHP Notice: Undefined offset: 0 in /home/martin/php/osm100.php on line 52
#22: ID:2755576944 [39.3641541,17.1310900] (unnamed)
<br/>martin@linux-70ce:~/php>



i have to futher investigaions....
unleash
 
Posts: 147
Joined: 03. December 2011 10:16
Operating System: OpenSuse Linux 12.1


Return to Allerlei

Who is online

Users browsing this forum: No registered users and 9 guests