I want to change SLD of wms layer dynamically. In SLD string I have CssParameter XML tag that it 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
Thant it refer to line that it has # charachter. 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 });
You can Convert Hex value of color to base 10. for example use 4359924 (BASE 10) instead of #4286f4 (BASE 16) in CssParameter tag: