XMLCoder Documentation

Class XMLEncoder

open class XMLEncoder  

XMLEncoder facilitates the encoding of Encodable values into XML.

Nested Types

XMLEncoder.OutputFormatting

The formatting of the output XML data.

XMLEncoder.PrettyPrintIndentation

The indentation to use when XML is pretty-printed.

XMLEncoder.NodeEncoding

A node's encoding type. Specifies how a node will be encoded.

XMLEncoder.DateEncodingStrategy

The strategy to use for encoding Date values.

XMLEncoder.StringEncodingStrategy

The strategy to use for encoding String values.

XMLEncoder.DataEncodingStrategy

The strategy to use for encoding Data values.

XMLEncoder.NonConformingFloatEncodingStrategy

The strategy to use for non-XML-conforming floating-point values (IEEE 754 infinity and NaN).

XMLEncoder.KeyEncodingStrategy

The strategy to use for automatically changing the value of keys before encoding.

XMLEncoder.NodeEncodingStrategy

Set of strategies to use for encoding of nodes.

Nested Type Aliases

Node​Encoding​Strategies

@available(*, deprecated, renamed: "NodeEncodingStrategy")
    public typealias NodeEncodingStrategies = NodeEncodingStrategy

XMLNode​Encoder​Closure

public typealias XMLNodeEncoderClosure = (CodingKey) -> NodeEncoding? 

XMLEncoding​Closure

public typealias XMLEncodingClosure = (Encodable.Type, Encoder) -> XMLNodeEncoderClosure

Initializers

init()

public init()  

Initializes self with default strategies.

Properties

characters​Escaped​InAttributes

open var charactersEscapedInAttributes = [
        ("&", "&"),
        ("<", "&lt;"),
        (">", "&gt;"),
        ("'", "&apos;"),
        ("\"", "&quot;"),
    ] 

Characters and their escaped representations to be escaped in attributes

characters​Escaped​InElements

open var charactersEscapedInElements = [
        ("&", "&amp;"),
        ("<", "&lt;"),
        (">", "&gt;"),
        ("'", "&apos;"),
        ("\"", "&quot;"),
    ] 

Characters and their escaped representations to be escaped in elements

output​Formatting

open var outputFormatting: OutputFormatting = [] 

The output format to produce. Defaults to [].

pretty​Print​Indentation

open var prettyPrintIndentation: PrettyPrintIndentation  

The indentation to use when XML is printed. Defaults to .spaces(4).

date​Encoding​Strategy

open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate

The strategy to use in encoding dates. Defaults to .deferredToDate.

data​Encoding​Strategy

open var dataEncodingStrategy: DataEncodingStrategy = .base64

The strategy to use in encoding binary data. Defaults to .base64.

non​Conforming​Float​Encoding​Strategy

open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw

The strategy to use in encoding non-conforming numbers. Defaults to .throw.

key​Encoding​Strategy

open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys

The strategy to use for encoding keys. Defaults to .useDefaultKeys.

node​Encoding​Strategy

open var nodeEncodingStrategy: NodeEncodingStrategy = .deferredToEncoder

The strategy to use in encoding encoding attributes. Defaults to .deferredToEncoder.

string​Encoding​Strategy

open var stringEncodingStrategy: StringEncodingStrategy = .deferredToString

The strategy to use in encoding strings. Defaults to .deferredToString.

user​Info

open var userInfo: [CodingUserInfoKey: Any] = [:] 

Contextual user-provided information for use during encoding.

Methods

encode(_:​with​Root​Key:​root​Attributes:​header:​doctype:​)

open func encode<T: Encodable>(_ value: T,
                                   withRootKey rootKey: String? = nil,
                                   rootAttributes: [String: String]? = nil,
                                   header: XMLHeader? = nil,
                                   doctype: XMLDocumentType? = nil) throws -> Data

Encodes the given top-level value and returns its XML representation.

Parameters

value T

The value to encode.

with​Root​Key String?

the key used to wrap the encoded values. The default value is inferred from the name of the root type.

root​Attributes [String:​ String]?

the list of attributes to be added to the root node

header XMLHeader?

the XML header to start the encoded data with.

Throws

An error if any value throws an error during encoding.

Returns

A new Data value containing the encoded XML data.

encode(_:​)

open func encode<T>(_ value: T) throws -> Data where T: Encodable