qsx

qsx

SweetXml: Parse into a list of mappings

Heya,

I’m trying to use SweetXml to parse an XML like this one:

<?xml version="1.0"?>
<lecturers>
  <lecturer>
    <key>75A7EBE3323C0EB1</key>
    <firstName>Albert</firstName>
    <lastName>Einstein</lastName>
  </lecturer>
  <lecturer>
    <key>619D3EDCF60E5A14</key>
    <firstName>Niels</firstName>
    <lastName>Bohr</lastName>
  </lecturer>
</lecturers>

… and I would like to get a list of mappings in return. Now, I actually already found a way to do so, but it seems needlessly complicated:

iex(26)> ls = """
...(26)> <?xml version="1.0"?>
...(26)> <lecturers>
...(26)>   <lecturer>
...(26)>     <key>75A7EBE3323C0EB1</key>
...(26)>     <firstName>Albert</firstName>
...(26)>     <lastName>Einstein</lastName>
...(26)>   </lecturer>
...(26)>   <lecturer>
...(26)>     <key>619D3EDCF60E5A14</key>
...(26)>     <firstName>Niels</firstName>
...(26)>     <lastName>Bohr</lastName>
...(26)>   </lecturer>
...(26)> </lecturers>
...(26)> """
"<?xml version=\"1.0\"?>\n<lecturers>\n  <lecturer>\n    <key>75A7EBE3323C0EB1</key>\n    <firstName>Albert</firstName>\n    <lastName>Einstein</lastName>\n  </lecturer>\n  <lecturer>\n    <key>619D3EDCF60E5A14</key>\n    <firstName>Niels</firstName>\n    <lastName>Bohr</lastName>\n  </lecturer>\n</lecturers>\n"
iex(27)> ls |> xpath(~x"/lecturers/lecturer"l, key: ~x"./key/text()"s, firstName: ~x"./firstName/text()"s, lastName: ~x"./lastName/text()"s)
[
  %{key: "75A7EBE3323C0EB1", firstName: "Albert", lastName: "Einstein"},
  %{key: "619D3EDCF60E5A14", firstName: "Niels", lastName: "Bohr"}
]

For every key/node, I have to specify it twice, and verbosely so (~x"./…/text()"s is a handful), while ideally I would like to do something like dwim(ls, ~x"/lecturers/lecturer"l, [:key, :firstName, :lastName]).

Am I just holding SweetXml wrong? Its documentation wasn’t really helpful to me, and I’m also a very novice Elixir programmer, so maybe I’m just overlooking something trivial.

Thanks!

First Post!

arcanemachine

arcanemachine

I’m pretty sure this is the least-verbose way to get what you want:

ls
# Parse a list of `lecturer` nodes
|> xpath(~x"./lecturer"l) 
# Parse the desired values from each node
|> Enum.map(fn node ->
  xmap(node,
    key: ~x"./key/text()"s,
    first_name: ~x"./firstName/text()"s,
    last_name: ~x"./lastName/text()"s)
  end)

For your example, this will produce:

[
  %{first_name: "Albert", key: "75A7EBE3323C0EB1", last_name: "Einstein"},
  %{first_name: "Niels", key: "619D3EDCF60E5A14", last_name: "Bohr"}
]

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement