How to parse XML

570 views
Skip to first unread message

Ben

unread,
May 9, 2016, 9:48:07 AM5/9/16
to Elm Discuss
Looking for a solution how to parse XML in Elm I couldn't find a definitive answer with a working example. From what I found [1,2] XML parsing seems to be related to JSON parsing. However, it seems there is no good way to do this yet.
Can someone tell me what is the state of the art way to parse a XML string in Elm?

At the moment I consider integrating x2js [3] as a native module, but I am quite new to Elm and don't know the best practices yet. What do you think?

Nick H

unread,
May 9, 2016, 10:00:28 AM5/9/16
to elm-d...@googlegroups.com
I believe you are correct, that there is no XML parser in Elm yet. (The redditor that claimed the JSON parser should "just work" with XML was talking gibberish.)

Fortunately, this means there is a great opportunity for somebody to contribute a useful package! This would need to be written in just Elm.

If you are looking for a fast solution, you could integrate with x2js via ports. But I don't think you wouldn't be able to publish it to the package repo.

Do not use native modules. They are a hack, and are soon going to be extinct.

~Nick

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick H

unread,
May 9, 2016, 12:13:54 PM5/9/16
to elm-d...@googlegroups.com
Sorry for the double negative. I meant, you won't be able to publish a module that uses ports.

Joost ter Braak

unread,
May 9, 2016, 3:57:58 PM5/9/16
to Elm Discuss
How is JSON-parsing and XML-parsing any different in Elm? in both cases you get an object from the browser which is already parsed and you only have to translate that to an Elm value. I will admit that is harder to "parse" XML in Elm as the attributes are more general.So you have to do a lot of poking to get the right nodes but it is doable.

Op maandag 9 mei 2016 18:13:54 UTC+2 schreef Nick H:

Ben

unread,
May 9, 2016, 4:52:57 PM5/9/16
to Elm Discuss
I think you are talking about different approaches here. Joost seems to propose the native path in analogy to the current implementation of Json.Decode, while Nick is in favor of having an Elm-only approach.

From what I read an Elm-only package would be a big thing to start. I see the following issues here:
(1) XML is tricky to implement correctly and I am not an expert here.
(2) There is no well tested Elm parser/lexer to build on as far as I know.
(3) From my experience, parsing XML is a task where performance issues arise quickly. So this should be considered from the start. To give an example, I intend to parse GPX files that may have a size of several MB with thousands of XML nodes.

In contrast, using native browser support gives you a tested XML parser with reasonable performance.

Correct me if I'm wrong here, but this is how I see it.

Evan Czaplicki

unread,
May 9, 2016, 5:16:16 PM5/9/16
to elm-d...@googlegroups.com
The correct answer is to use ports. Go to JS. Do what you need to do. Bring the values back into Elm.

Evan Czaplicki

unread,
May 9, 2016, 5:18:53 PM5/9/16
to elm-d...@googlegroups.com
Longer term, it makes sense to bring this into Elm. That's what Nick is talking about. It's not a good project for the community if you want to do JS stuff. Yes, it'd be hard to do it in Elm, so it may not be a good project for the community in general. You don't have to worry about any of that though. Just use ports for now.

Ben

unread,
May 10, 2016, 3:47:20 AM5/10/16
to Elm Discuss
Thank you for making this clear :-).

Tobias Burger

unread,
May 10, 2016, 10:30:01 AM5/10/16
to Elm Discuss
I've written a - very naive - XML parser using Bogdanp/elm-combine.
I was inspired by a XML parser example written in haskell and parsec. Sadly I can't find the original haskell code anymore.
I've extracted it from a larger project of mine, to get the gist: https://github.com/toburger/elm-xml-parser.git
For my simple needs it serves it's purpose quite well.
Reply all
Reply to author
Forward
0 new messages