It wasn't so obvious how to render XML in Phoenix with the new approach to live views and rendering of templates. This is quick guide on how to do so. This could be wrong at the moment of reading this but always refer to the internet.
In this post, I’ll guide you through the process of adding XML support to a Phoenix 1.7+ project. This is a consolidation of various forum insights, tailored for quick reference. Note that some details may become outdated, so always refer to the latest Phoenix documentation for updates.
Modifying the Web Module
First, update the FooWeb module to include XML in the list of supported formats:
#/lib/foo_web.exdefmoduleFooWebdo# .. omit for claritydefcontrollerdoquotedousePhoenix.Controller,formats:[:html,:json,:xml],# add :xml herelayouts:[html:FooWeb.Layouts]importPlug.ConnimportFooWeb.Gettextunquote(verified_routes())endend# .. omit for clarityend
Creating the controller
Next, create an RSS controller to handle XML responses:
# /lib/foo_web/controllers/rss_xml/rss.xml.eex<?xmlversion="1.0"encoding="UTF-8"?><rssversion="2.0"><channel>#.. omit for clarity. You put whatever XML tags you need here</channel></rss>
Updating the Router
Finally, update the router to direct requests to the RSS controller: