Elm:
Working with XML
How to:
In Elm, you deal with XML using the elm/xml
package. Here’s a quick look at parsing an XML snippet:
import Xml.Decode exposing (..)
import Xml.Decode.Pipeline exposing (..)
xmlString = """
<book id="123">
<title>Elm in Action</title>
<author>Robin Heggelund Hansen</author>
</book>
"""
type alias Book =
{ id : String
, title : String
, author : String
}
bookDecoder : Decoder Book
bookDecoder =
decode Book
|> required "id" (attribute "id")
|> required "title" (child "title" (content text))
|> required "author" (child "author" (content text))
case Xml.Decode.fromString bookDecoder xmlString of
Ok book ->
-- Do something with the decoded book here
Debug.toString book
Err error ->
-- Handle errors
Debug.toString error
Sample output, assuming no errors:
"{ id = \"123\", title = \"Elm in Action\", author = \"Robin Heggelund Hansen\" }"
Deep Dive
XML (eXtensible Markup Language) has been around since the late 90s, a time when the web was text-heavy and the need for a structured, yet flexible way to carry data was crucial. Due to verbosity and complexity, XML has lost some ground to JSON. However, XML is still prevalent, especially in enterprise environments or protocols like SOAP.
Elm’s approach to XML is functional and type-safe. Using the elm/xml
package means embracing the Elm philosophy of explicitness and reliability. When it comes to parsing, the package provides a range of decoders which you compose to handle the XML structure.
Compared to alternatives like JavaScript’s DOMParser or Python’s ElementTree, Elm’s method might seem more verbose but ensures safety. No runtime exceptions for missing fields or type mismatches; if something’s amiss, you get a compile-time error.
The elm/xml
decode functions hinge on mapping XML nodes to Elm types. You build decoders that mirror your data’s shape, ensuring your Elm app handles XML as rigorously as it does its own internal data structures.
Generation of XML is less common in Elm but can be achieved with elm/xml
’s counterpart Xml.Encode
.
See Also
- Elm Guide on JSON which also applies to XML mindset: https://guide.elm-lang.org/interop/json.html
- XML standard by W3C for a deeper understanding of XML itself: https://www.w3.org/XML/