If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below. ** If you are logged in, most ads will not be displayed. **

php simpleXML :parsing an osm-file

I am new to PHP's SimpleXML.

The original version of this question was derived from here: OSM Data parsing to get the nodes with child

I am thankful that hakre offered a great example in the comments that makes a overwhelming starting point for my project. 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.

The link gave a description of the API you're using. If you wish to program in PHP you need to understand at least a minimum of the language and that is where you should start. You should read up on xml and xpath and try to understand how the API exposes the data.

One starting point can be: Start by examining the entire XML object.
Seeing what nodes exist and what they are called makes it much easier to create the search string.

Code:

echo "<pre>";
var_dump($result);
echo "</pre>";

well i am learing - and this code snippets make me learn alot.

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):

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:

//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:

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:

OpenStreetMap provides geo-coded information in the form of extracted .osm files for download.
.osm files are XML files with a defined schema, but this information alone does not really help
in analyzing the files. The reason is that the simple structure <node><tag /></node> distinguishes
its data by the tag’s k attribute value:

"simplexml_load_file" loads the entire document into a resource of the type SimpleXMLElement. A SimpleXMLElement is Traversable therefore it can be accessed with foreach. It also has a number of methods to allow access - one of with is xpath. This takes an xpath query as its argument. In the documentation it show exactly one argument so your error message is very understandable. If your goal i to load the entire document that's all it takes.It also has methods to add elements.

I believe You are going at this wrong. Reading other peoples code is an important skill, however, taking that code without understanding it and trying to hack together a working program is seldom a good idea.