The Geography Markup Language (GML) is an XML grammar that is used to express geographic features. It is an OGC standard and is very widely accepted by the GIS community.
In this recipe (the source code is in ch03/ch03-gml-layer/), we will show you how to create a vector layer from a GML file which can be seen in the following screenshot:

In order to import features in GML format into the map, follow these instructions:
div element to hold the map:<div id="js-map"></div>
var map = new ol.Map({
view: new ol.View({
zoom: 4,
center: [-7494000, 2240000]
}),
target: 'js-map',
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'osm'})
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'bermuda-triangle.gml',
format: new ol.format.GML2()
})
})
]
});Let's take a look at the vector layer instantiation in detail:
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'bermuda-triangle.gml',
format: new ol.format.GML2()
})
})Just as raster layers require a source, as seen in Chapter 2, Adding Raster Layers, so do vector layers. Vector layers use the ol.source.Vector class in order to present features.
We provide an HTTP endpoint as a string to the url property. OpenLayers will make an AJAX request on our behalf for this URL, which contains the GML file.
We inform OpenLayers what the format of the source is by setting the format property to GML2. The GML source is at version 2.1.2, which ol.format.GML2 can read and process. For GML 3.1.1, use the ol.format.GML or ol.format.GML3 constructors.
In this example, the vector layer source request will load all of the features from the source on to the map and make no further AJAX requests, implicitly using the ol.loadingstrategy.all strategy. However, there is a property of ol.source.Vector called strategy, which can be used to specify an alternative feature loading strategy, such as loading features only for the extent of the view (ol.loadingstrategy.bbox).
On receiving the GML source, OpenLayers reads the GML-formatted features and converts each feature into a format that can be used on the map (ol.Feature).
OpenLayers offers many other formats to read/write data, but in this recipe, we made use of the ol.format.GML2 instance because our data source is a GML version 2 file.