XML
Parsing
Serde.ParXml.parse_xml
— Functionparse_xml(x::AbstractString; kw...) -> Dict{String,Any}
parse_xml(x::Vector{UInt8}; kw...) -> Dict{String,Any}
Parse an XML string x
(or vector of UInt8) into a dictionary.
Keyword arguments
dict_type::Type{<:AbstractDict} = Dict
: The type of the dictionary to be returned.
Examples
julia> xml = """
<book id="bk101">
<title>Advanced Julia Programming</title>
<authors>
<author lang="en">John Doe</author>
<author lang="es">Juan Pérez</author>
</authors>
<year>2024</year>
<price>49.99</price>
</book>
"""
julia> parse_xml(xml)
Dict{String, Any} with 5 entries:
"price" => Dict{String, Any}("_"=>"49.99")
"year" => Dict{String, Any}("_"=>"2024")
"id" => "bk101"
"title" => Dict{String, Any}("_"=>"Advanced Julia Programming")
"authors" => Dict{String, Any}("author"=>Dict{String, Any}[Dict("lang"=>"en", "_"=>"John Doe"), Dict("lang"=>"es", "_"=>"Juan Pérez")])
Serde.ParXml.XmlSyntaxError
— TypeXmlSyntaxError <: Exception
Exception thrown when a parse_xml
fails due to incorrect XML syntax or any underlying error that occurs during parsing.
Fields
message::String
: The error message.exception::Exception
: The exception that was caught.
Deserialization
Serde.DeXml.deser_xml
— Functiondeser_xml(::Type{T}, x; kw...) -> T
Creates a new object of type T
and fill it with values from XML formated string x
(or vector of UInt8).
Keyword arguments kw
is the same as in parse_xml
.
Examples
julia> struct Record
count::Float64
end
julia> struct Data
id::Int64
name::String
body::Record
end
julia> xml = """
<root>
<id>100</id>
<name>xml</name>
<body>
<count>100.0</count>
</body>
</root>
""";
julia> deser_xml(Data, xml)
Data(100, "xml", Record(100.0))
Serialization
Serde.SerXml.to_xml
— Functionto_xml(val; key::String = "xml") -> String
Serializes any nested data val
into an XML string that follows the next rules:
- Values of primitive types are used as an element of the current tag.
- Vector elements will be used as sub-tag elements.
- Dictionaries are processed using the following rules:
- Key names must be a string or a symbol types.
- A key with a non-empty string value will be interpreted as a new sub-tag.
- A key with an empty string value will be interpreted as an element of the current tag.
- Custom types are handled as follows:
- The field name containing the primitive type will be used as an attribute for the current tag.
- A field name containing a composite type (dictionary or other custom type) will be used as the name for the next sub-tag.
- A primitive type field with a special name "_" will be used as an element for the current tag.
Thus, this method can serialize all basic data types and can work with any nesting level of a combination of dictionaries and custom data types. The key
keyword specifies the name of the root tag.
Examples
julia> struct Image
dpi::Int64
_::String
end
julia> struct Data
info::Dict
image::Image
end
julia> data_info = Dict("id" => "451", "status" => "OK", "_" => "employee");
julia> to_xml(Data(data_info, Image(200, "profile.png"))) |> print
<xml>
<image dpi="200">profile.png</image>
<info status="OK" id="451">employee</info>
</xml>