I want to change SLD of WMS layer dynamically. In SLD string I have CssParameter XML tag that has Hex color code like "#ff0000". When I post it to GeoServer I got this error:
java.lang.RuntimeException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 619; XML document structures must start and end within the same entity. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 619; XML document structures must start and end within the same entity. XML document structures must start and end within the same entity
That refers to line the line that it has the #
character. Here is the code sample:
var SLD = '<?xml version="1.0" encoding="UTF-8"?> <StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:se="http://www.opengis.net/se">';
SLD += '<NamedLayer><Name>pmo:iczmoagriculturemokran</Name><UserStyle><Name>pmo:iczmoagriculturemokran</Name><Title>Green polygon</Title><Abstract>Green fill with black outline</Abstract><FeatureTypeStyle><Rule><PolygonSymbolizer><Fill><CssParameter name="fill">#ff0000</CssParameter></Fill><Stroke /></PolygonSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>';
SLD = encodeURI(SLD);
var source = layer.WMSLayer.getSource();
source.updateParams({ STYLES: undefined, SLD_BODY: SLD });
After encodeURI SLD turns to:
%3C?xml%20version=%221.0%22%20encoding=%22UTF-8%22?%3E%3CStyledLayerDescriptor%20version=%221.0.0%22%20xmlns=%22http://www.opengis.net/sld%22%20xmlns:ogc=%22http://www.opengis.net/ogc%22%20xmlns:xlink=%22http://www.w3.org/1999/xlink%22%20xmlns:xsi=%22http://www.w3.org/2001/XMLSchema-instance%22%20xsi:schemaLocation=%22http://www.opengis.net/sld%20http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3Egreen%3C/Name%3E%3CUserStyle%3E%3CName%3Egreen%3C/Name%3E%3CTitle%3EGreen%20polygon%3C/Title%3E%3CAbstract%3EGreen%20fill%20with%20black%20outline%3C/Abstract%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter%20name=%22fill%22%3E#ff0000%3C/CssParameter%3E%3C/Fill%3E%3CStroke%20/%3E%3C/PolygonSymbolizer%3E%3C/Rule%3E%3C/FeatureTypeStyle%3E%3C/UserStyle%3E%3C/NamedLayer%3E%3C/StyledLayerDescriptor%3E