Skip to content

Repository files navigation

XMLCoding

Encoding

Usage

Here is an example of XMLEncoder usage. For given input:

enumHabit:String,Encodable{case sports
case videogames
case music
case movies
case series
}structPerson:Encodable{letage:UIntletname:Stringlethabits:[Habit]}letvalue=Person(
age:25,
name:"Vitaliy",
habits:[.movies,.music,.sports
])letxmlEncoder=XMLEncoder()letresult=try! xmlEncoder.encode(value)

the result will be:

<persontype="Person">
<age>25</age>
<name>Vitaliy</name>
<habitstype="Array"elementType="Habit">
<itemtype="Habit">
movies
</item>
<itemtype="Habit">
music
</item>
<itemtype="Habit">
sports
</item>
</habits>
</person>

Customization

XMLEncoder can receive two customizable properties in its init method:

publicfinalclassXMLEncoder{publicinit(
configuration:XMLEncodingConfiguration=XMLEncodingConfiguration.default,
attributesWorker:BuildsAttributesString=AttributesStringBuilder())...}

The first property configuration of type XMLEncodingConfiguration allows you to customize the number of spaces in indents, the root element name (type name is used by default) and whether encoder should include type attributes or not. Example outputs for different configurations can be found here and here.

BuildsAttributesString is an interface that allows encoder to insert attributes for encoded values. AttributesStringBuilder is a default implementation of this protocol that, it adds type and elementType attributes as shown above.

About

Serialization package that allows user to encode values into XML format

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages