XML

Parsing

Serde.SerdeXml.parse_xmlFunction
parse_xml(x::Union{AbstractString, Vector{UInt8}}; dict_type = Dict{String,Any}, force_array = false, kw...) -> Dict

Parse an XML string or byte vector into a nested Dict without mapping to a target type.

XML attributes and child elements are both represented as dict entries. Text content of a node is stored under the special key "_". When the same child element name appears multiple times, the values are collected into a Vector.

Arguments

  • x: XML text as a String or Vector{UInt8}.

Keyword arguments

  • dict_type::Type{<:AbstractDict}: concrete dict type for nodes (default Dict{String,Any}).
  • force_array::Bool = false: when true, all child elements are wrapped in Vector even if they appear only once.

Returns

A nested dict representing the root XML element.

Throws

Examples

julia> parse_xml("<root><x>1</x><y>2</y></root>")
Dict{String, Any}("x" => Dict{String, Any}("_" => "1"), "y" => Dict{String, Any}("_" => "2"))

See also: from_xml.

source

Deserialization

Serde.SerdeXml.from_xmlFunction
from_xml(::Type{T}, x; kw...) -> T
from_xml(strategy, ::Type{T}, x; kw...) -> T
from_xml(f::Function, x; kw...) -> Any

Parse an XML string and deserialize the root element into type T.

The XML is first parsed into a nested dict via parse_xml, then deserialized using Serde.deser. XML attributes and child elements are accessible as dict keys. Text content of the root element is available under the key "_".

Pass a context object strategy (e.g. CamelCase()) to apply global field-renaming.

Arguments

  • ::Type{T}: target type to construct.
  • x: XML text as a String or Vector{UInt8}.
  • strategy: optional context object.

Returns

A value of type T.

Throws

Examples

julia> struct Point; x::Int; y::Int; end

julia> from_xml(Point, "<root x="1" y="2"/>")
Point(1, 2)

See also: to_xml, parse_xml.

source

Serialization

Serde.SerdeXml.to_xmlFunction
to_xml(val; key::String = "xml") -> String
to_xml(strategy, val; key::String = "xml") -> String

Serialize val into an XML string.

The value is wrapped in a root element named by key. Scalar (primitive) struct fields are serialized as XML attributes of the wrapping element. Nested structs produce nested child elements. nothing and missing fields are omitted. Vector fields produce repeated sibling elements with the same tag name.

Pass a context object strategy (e.g. CamelCase()) to apply global field-renaming.

All serialization traits (Serde.ser_name, Serde.ser_value, Serde.ser_skip) are applied.

Arguments

  • val: the value to serialize.
  • strategy: optional context object.

Keyword arguments

  • key::String = "xml": name of the root XML element.

Returns

An XML-formatted String.

Examples

julia> struct Point; x::Int; y::Int; end

julia> to_xml(Point(1, 2)) |> print
<xml x="1" y="2"/>

julia> to_xml(Point(1, 2); key="point") |> print
<point x="1" y="2"/>

See also: from_xml.

source