silverdr

silverdr

Static XML to Elixir "term"

There’s a fairly large data structure represented as an XML file. I don’t feel like parsing said file every time I need a piece of data out of it represented in Elixir terms. Since the data for current intent and purpose can be treated as static I thought of “compiling it in” so that it could be accessed quickly and without re–parsing the XML. But before I start dusting off my sed-fu to convert text files, maybe the Elixir experts community have something better/ready for such occasions? How do you typically pull such external data into Elixir applications?

Marked As Solved

al2o3cr

al2o3cr

The @external_resource module attribute is a good place to start:

You could point that at your XML file, then parse it into a module attribute:

defmodule BagOfXml do
  @external_resource "path/to/some.xml"

  @parsed_data SomeOtherModule.parse("path/to/some.xml")

  def lookup_thing(id) do
    # use @parsed_data and id to do stuff
  end
end

This will call SomeOtherModule.parse at compile-time (it cannot be a function in the same file) and store the resulting data in the compiled BEAM file.

@external_resource will ensure that if the XML file changes the module will be recompiled.

Also Liked

LostKobrakai

LostKobrakai

If things are static enough I usually parse the external file at compile time and pull only the parts I need in a module attribute.

LostKobrakai

LostKobrakai

Essentially that, but also if only parts of the data are needed at runtime, discarding everything not needed. Nobody wants unreasonable large beam files.

silverdr

silverdr

Could you be so kind as to elaborate a bit on that?

Last Post!

silverdr

silverdr

I suspected there must be something better! This looks exactly right for the purpose. The fact that it will recompile once the data file changes (rarely but still can’t be fully excluded) is an icing on the cake. Thanks guys.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement