Using Elm for parsing deeply nested XML

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?

Hi,

I haven’t tried these myself, but perhaps these two packages are worth looking at;
https://package.elm-lang.org/packages/billstclair/elm-xml-eeue56/latest/
https://package.elm-lang.org/packages/ymtszw/elm-xml-decode/latest/

Looks like the last one has gone for an API similar to Elm’s JSON library. Might be helpful.

Edit: I wasn’t able to view the XML example you linked to. Don’t have an account there.

I already tried those libraries, but I probably still need to use one of those. I just need to know exactly how I can use them for the type of XML format that I’m dealing with here. But thanks for trying to be helpful. Already having a better experience with this site than with StackOverflow.

I will try putting the XML code on some other website. I can’t put it on GitHub because my account there is flagged (probably because of a repository I created that said something like “star this repo or else you are gay”), and I don’t want to put the XML code directly in here because it’s very large.

Edit: I put the file on Google Drive: https://drive.google.com/file/d/1LXUuqbvwnsSHEZHaKu8BtFcvwXKgY3j3/view?usp=sharing

I’m figuring it out now. I started rewriting my parser several hours ago and it’s going well. I’m using function pipelines to do it. But I am having a problem with records. I will make a separate post about that. Again, thanks for helping when Stack Overflow didn’t.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.