Web App Development

How to convert an OpenLayers filter object to a CQL XML string for GeoServer

You first need to convert the object to a filter node and then to XML:
filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "prop",
value: "val"
// OpenLayers.Filter.Comparison.OpenLayers.Class.initialize {type: "==", property: "prop", value: "val", type: null, property: null…}
node = new OpenLayers.Format.Filter({version: "1.1.0"}).write(filter);
// <ogc:filter>​…​</ogc:filter>​
xml = new OpenLayers.Format.XML().write(node)
// "<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsEqualTo matchCase="true"><ogc:PropertyName>prop</ogc:PropertyName><ogc:Literal>val</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>"
You can then pass the xml string as a filter argument to the map layer in OpenLayers:

new OpenLayers.Layer.WMS(
filter: '<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsEqualTo matchCase="true"><ogc:PropertyName>prop</ogc:PropertyName><ogc:Literal>val</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>';


Follow me on Twitter
I'm building monitoring tool for site speed and Core Web Vitals.
➔ Start monitoring your website or run a free site speed test