I’m creating a browser-based world edit tool for Survivalcraft, and I have to parse XML using Elm. You can see an example of the XML format here.
-
The root element of the XML has 2 children: and .
-
contains many elements.
-
and each can contain and elements.
-
has Name, Type, and Value attributes.
-
has a Name attribute and contains and elements.
I defined some types that I want to use to represent the file:
type XMLItem -- Represents either a Value or Values XML tag
= Values String (List XMLItem) -- name and list of xml items
| ValueBool String Bool
| ValueInt String Int
| ValueLong String Int
| ValueFloat String Float
| ValueDouble String Float
| ValuePoint3 String Int Int Int
| ValueVector3 String Float Float Float
| ValueQuaternion String Float Float Float Float
| ValueString String String
| ValueGameMode String GameMode
| ValuePlayerClass String PlayerType
type alias ProjectFile = -- Represents the whole file
{ subsystems : List XMLItem
, entities : List ProjectEntity
, version : GameVersion
, guid : String
}
type alias ProjectEntity = -- Represents an Entity tag
{ id : Int
, guid : String
, name : String
, content : List XMLItem -- The child nodes
}
I want to make a function that takes the contents of the XML file as a string and returns a ProjectFile record. I tried doing it, but the implementation I came up with was not finished and it’s too complex. I deleted the code and I don’t have it anywhere anymore. What is the best way to implement a function that does this?