ymtszw/elm-xml-decode

Xml decoder sharing the spirit of Json.Decode


Keywords
decoder, elm, elm-lang, xml
License
BSD-3-Clause
Install
elm-package install ymtszw/elm-xml-decode 1.0.1

Documentation

elm-xml-decode

GitHub package version license TravisCI CircleCI

XML decoder, sharing the spirit of Json.Decode.

Using jinjor/elm-xml-parser as its parser component.

Related Works

eeue56/elm-xml is an existing full-package XML parser/decoder for Elm, though I intend to provide an alternative XML decoder which exhibits following properties:

Examples

Basics:

import Xml.Decode exposing (Decoder, map2, path, single, list, string, int)


type alias Example =
    { string : String
    , integers : List Int
    }


exampleDecoder : Decoder Example
exampleDecoder =
    map2 Example
        (path [ "path", "to", "string", "value" ] (single string))
        (path [ "path", "to", "int", "values" ] (list int))

Execute decode:

Xml.Decode.run exampleDecoder
    """
    <root>
        <path>
            <to>
                <string>
                    <value>SomeString</value>
                </string>
                <int>
                    <values>1</values>
                    <values>2</values>
                </int>
            </to>
        </path>
    </root>
    """
--> Ok { string = "SomeString", integers = [ 1, 2 ] } : Example

Decoder compositions

We have map, map2 and variants, though the following methods are also possible.

Pipeline style!:

import Xml.Decode exposing (Decoder, succeed, single, list, string, int)
import Xml.Decode.Pipeline exposing (requiredPath)


exampleDecoder : Decoder Example
exampleDecoder =
    succeed Example
        |> requiredPath [ "path", "to", "string", "value" ] (single string)
        |> requiredPath [ "path", "to", "int", "values" ] (list int)

Applicative style!:

import Xml.Decode exposing (Decoder, succeed, path, single, list, string, int)
import Xml.Decode.Extra exposing ((|:))

exampleDecoder : Decoder Example
exampleDecoder =
    succeed Example
        |: path [ "path", "to", "string", "value" ] (single string)
        |: path [ "path", "to", "int", "values" ] (list int)

Development

npm install
npm test
npm run watch:test # Monitor file changes and update doc-test
npm run docs       # Compile and generate docs.json
npm run analyse    # Analyse Elm code

Benchmark: Are they efficient? Are they fast?

Benchmark codes can be found in benchmarks/ directory. Using BrianHicks/elm-benchmark and examples in W3School. Available here as a static web page.

Develop:

npm run bench -- init
npm run bench -- compile
open docs/index.html

Sample result (on my MacBookPro early 2015):

  • CPU: Core i5 2.7GHz
  • Mem: DDR3 8GB 1867MHz
  • Google Chrome 63.0.3239.84 64bit

bench

jinjor/elm-xml-parser is using elm-tools/parser, which currently suffers from some performance issues (possibly related to this comment?). We hope the underlying issue will be resolved in future versions of Elm and/or those dependencies.

Although decoding part should practically be fast enough (see decode result of note.xml and cd_catalog.xml).

Looking forward to see similar benchmarks from other related works!!

License

BSD-3-Clause