elixir-book/weather/lib/weather/nice_formatter.ex

24 lines
642 B
Elixir

defmodule Weather.NiceXMLFormatter do
require Weather.XML
def format_observation_nicely(xml_string) when is_binary(xml_string) do
{doc, []} =
xml_string
|> to_charlist()
|> :xmerl_scan.string()
location = get_child("location", doc)
weather = get_child("weather", doc)
observation_time = get_child("observation_time", doc)
IO.puts "Location: #{location}"
IO.puts "Weather: #{weather}"
IO.puts observation_time
end
defp get_child(node_name, doc) do
:xmerl_xpath.string('/current_observation/#{node_name}/text()', doc)
|> Enum.map(&Weather.XML.xmlText(&1, :value))
end
end