Best way to generate XML libraries?

So when I would be building XML API (not JSON API) what do you recommend I use?


Check these out:



I ended up with the simplest solution, probably: EEX template.


Could you share some info about how you set up your controller to properly serve xml?

1 Like

Pretty identically to json, you’ll just need to change the json type to xml, name the templates properly, then just fill them out, or like html you can do it as text templates. :slight_smile:

Look in the ‘View(s)’ documentation for Phoenix for details. :slight_smile:

1 Like

I guess I could do something like:
render api_response.xml.eex

Then in my api_response.xml.eex, do I just build up the xml the way I would with html?

That is one way yep. Or you can make a custom render function that builds up valid xml via one of the xml libraries and returns it as an iolist or so? :slight_smile:

1 Like

Neat. I’ll have to put in a PR to the docs if I eventually put together something clean.

1 Like

So what I came up with was something like:

def create(conn, _params) do
    {:ok, data, _conn_details} = Plug.Conn.read_body(conn)
    thing =
        |> Integration.build_the_thing
    xml = Thing.to_xml(thing)
     |> put_layout(:xml)
     |> put_resp_content_type("text/xml")
     |> render("thing_xml.xml", xml: xml)

Then a thing_xml.xml.eex template something like this:

    <%= @thing_xml %>

I’m parsing conn body with sweet_xml. It all works but its like a 300ms round trip so I guess I need to work on the specifics a bit, because 300ms seems a touch slow for this.

1 Like

Are you in the ‘dev’ environment? If so then that is why as it checks for and recompiles everything needed on every request, even if no recompiles are needed then it still slows it down because of the checks. Prod mode is much faster.

Plus make sure you are not on windows, it adds a delay regardless because the windows networking stack sucks. ^.^

1 Like