EctoXml.to_xml
You're seeing just the function
to_xml
, go back to EctoXml module for more information.
Link to this function
to_xml(data, document_name, options \\ [])
Specs
Generates a XML document based on one ecto schema or map.
The document_name
argument refers to the XML root element name.
Generating XML from maps
The following:
%{foo: "bar"} |> EctoXml.to_xml(:root, format: :none)
Results in:
<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>
Generating XML from an Ecto Schema
For a given schema:
defmodule Person do
@moduledoc false
use Ecto.Schema
@primary_key false
embedded_schema do
field :name, :string
end
end
s The following:
%Person{name: "Foo Bar"} |> EctoXml.to_xml(:person, format: :none)
Results in:
<?xml version="1.0" encoding="UTF-8"?><person><name>Foo Bar</name></person>
Customizing field names
For a given schema:
defmodule Person do
@moduledoc false
use Ecto.Schema
@primary_key false
@derive {
EctoXml.Builder,
map_field_names: %{
:name => :custom_element_name
}
}
embedded_schema do
field :name, :string
end
end
The following:
%Person{name: "Foo Bar"} |> EctoXml.to_xml(:person, format: :none)
Results in:
<?xml version="1.0" encoding="UTF-8"?><person><custom_element_name>Foo Bar</custom_element_name></person>