Xml in Elm

Here’s the translation of the XML example from Go to Elm:

module Main exposing (main)

import Html exposing (Html, div, text)
import Json.Encode as Encode
import Json.Decode as Decode
import Json.Decode.Pipeline as Pipeline

type alias Plant =
    { id : Int
    , name : String
    , origin : List String
    }

plantEncoder : Plant -> Encode.Value
plantEncoder plant =
    Encode.object
        [ ("id", Encode.int plant.id)
        , ("name", Encode.string plant.name)
        , ("origin", Encode.list Encode.string plant.origin)
        ]

plantDecoder : Decode.Decoder Plant
plantDecoder =
    Decode.succeed Plant
        |> Pipeline.required "id" Decode.int
        |> Pipeline.required "name" Decode.string
        |> Pipeline.required "origin" (Decode.list Decode.string)

plantToString : Plant -> String
plantToString plant =
    "Plant id=" ++ String.fromInt plant.id ++ ", name=" ++ plant.name ++ ", origin=" ++ String.join ", " plant.origin

main : Html msg
main =
    let
        coffee =
            { id = 27
            , name = "Coffee"
            , origin = ["Ethiopia", "Brazil"]
            }

        encodedCoffee =
            Encode.encode 2 (plantEncoder coffee)

        decodedCoffee =
            case Decode.decodeString plantDecoder encodedCoffee of
                Ok plant ->
                    plantToString plant

                Err _ ->
                    "Error decoding plant"

        tomato =
            { id = 81
            , name = "Tomato"
            , origin = ["Mexico", "California"]
            }

        nesting =
            { plants = [coffee, tomato] }

        encodedNesting =
            Encode.encode 2 (Encode.object
                [ ( "nesting"
                  , Encode.object
                        [ ( "parent"
                          , Encode.object
                                [ ( "child"
                                  , Encode.list plantEncoder nesting.plants
                                  )
                                ]
                          )
                        ]
                  )
                ]
            )
    in
    div []
        [ text "Encoded Coffee:"
        , div [] [text encodedCoffee]
        , text "Decoded Coffee:"
        , div [] [text decodedCoffee]
        , text "Encoded Nesting:"
        , div [] [text encodedNesting]
        ]

This Elm code provides similar functionality to the original XML example, but uses JSON for encoding and decoding instead of XML, as Elm doesn’t have built-in XML support. Here’s a breakdown of the changes:

  1. We define a Plant type alias instead of a struct.

  2. Instead of XML tags, we use JSON encoders and decoders. The plantEncoder and plantDecoder functions handle conversion between Plant records and JSON.

  3. The plantToString function provides similar functionality to the String() method in the original code.

  4. In the main function, we create sample Plant records and demonstrate encoding and decoding.

  5. For the nesting example, we create a similar structure using nested JSON objects.

  6. The output is displayed using Elm’s Html module instead of printing to console.

Note that this example doesn’t include an exact equivalent of XML attributes or the XML header. In JSON, all data is represented as key-value pairs within objects.

To run this Elm program, you would typically compile it to JavaScript and run it in a web browser. The output would be displayed on the web page rather than in a console.