XML encoder for Elm with CDATA support.
This is a fork of billstclair/elm-xml-eeue56 that is focused on only XML encoding functionality and supporting CDATA tags.
elm install dillonkearns/elm-xml-encodeimport Xml.Encode exposing (..)
import Dict
xmlDocument : String
xmlDocument =
object
[ ( "root", Dict.empty, string "Hello, World!" ) ]
|> encode 0
-- Result: "<root>Hello, World!</root>"Use cdata to embed HTML or other XML content without entity escaping:
import Xml.Encode exposing (..)
import Dict
-- Regular string encoding (entities are escaped)
regularContent : String
regularContent =
object
[ ( "description", Dict.empty, string "<p>HTML & symbols</p>" ) ]
|> encode 0
-- Result: "<description><p>HTML & symbols</p></description>"
-- CDATA encoding (content preserved as-is)
cdataContent : String
cdataContent =
object
[ ( "description", Dict.empty, cdata "<p>HTML & symbols</p>" ) ]
|> encode 0
-- Result: "<description><![CDATA[<p>HTML & symbols</p>]]></description>"
-- CDATA automatically handles problematic sequences
problematicContent : String
problematicContent =
object
[ ( "content", Dict.empty, cdata "Text with ]]> sequence" ) ]
|> encode 0
-- Result: "<content><![CDATA[Text with ]]]]><![CDATA[> sequence]]></content>"See the package documentation for detailed API information.