@@ -290,6 +290,61 @@ func fetchBook(from url: URL) -> AnyPublisher<Book, Error> {
290290This was implemented in PR [ \# 132] ( https://github.com/MaxDesiatov/XMLCoder/pull/132 )
291291by [ @sharplet ] ( https://github.com/sharplet ) .
292292
293+ Additionally, starting with [ XMLCoder
294+ 0.11] ( https://github.com/MaxDesiatov/XMLCoder/releases/tag/0.11.0 ) ` XMLEncoder `
295+ conforms to the ` TopLevelEncoder ` protocol:
296+
297+ ``` swift
298+ import Combine
299+ import XMLCoder
300+
301+ func encode (book : Book) -> AnyPublisher<Data, Error > {
302+ return Just (book)
303+ .encode (encoder : XMLEncoder ())
304+ .eraseToAnyPublisher ()
305+ }
306+ ```
307+
308+ The resulting XML in the example above will start with ` <book ` , to customize
309+ capitalization of the root element (e.g. ` <Book ` ) you'll need to set an
310+ appropriate ` keyEncoding ` strategy on the encoder. To change the element name
311+ altogether you'll have to change the name of the type, which is an unfortunate
312+ limitation of the ` TopLevelEncoder ` API.
313+
314+ ### Root element attributes
315+
316+ Sometimes you need to set attributes on the root element, which aren't
317+ directly related to your model type. Starting with [ XMLCoder
318+ 0.11] ( https://github.com/MaxDesiatov/XMLCoder/releases/tag/0.11.0 ) the ` encode `
319+ function on ` XMLEncoder ` accepts a new ` rootAttributes ` argument to help with
320+ this:
321+
322+ ``` swift
323+ struct Policy : Encodable {
324+ var name: String
325+ }
326+
327+ let encoder = XMLEncoder ()
328+ let data = try encoder.encode (Policy (name : " test" ), rootAttributes : [
329+ " xmlns" : " http://www.nrf-arts.org/IXRetail/namespace" ,
330+ " xmlns:xsd" : " http://www.w3.org/2001/XMLSchema" ,
331+ " xmlns:xsi" : " http://www.w3.org/2001/XMLSchema-instance" ,
332+ ])
333+ ```
334+
335+ The resulting XML will look like this:
336+
337+ ``` xml
338+ <policy xmlns =" http://www.nrf-arts.org/IXRetail/namespace"
339+ xmlns : xsd =" http://www.w3.org/2001/XMLSchema"
340+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
341+ <name >test</name >
342+ </policy >
343+ ```
344+
345+ This was implemented in PR [ \# 160] ( https://github.com/MaxDesiatov/XMLCoder/pull/160 )
346+ by [ @portellaa ] ( https://github.com/portellaa ) .
347+
293348## Installation
294349
295350### Requirements
@@ -316,7 +371,7 @@ easy as adding it to the `dependencies` value of your `Package.swift`.
316371
317372``` swift
318373dependencies: [
319- .package (url : " https://github.com/MaxDesiatov/XMLCoder.git" , from : " 0.10 .0" )
374+ .package (url : " https://github.com/MaxDesiatov/XMLCoder.git" , from : " 0.11 .0" )
320375]
321376```
322377
@@ -347,7 +402,7 @@ target 'YourApp' do
347402 use_frameworks!
348403
349404 # Pods for YourApp
350- pod ' XMLCoder' , ' ~> 0.10 .0'
405+ pod ' XMLCoder' , ' ~> 0.11 .0'
351406end
352407```
353408
@@ -376,7 +431,7 @@ $ brew install carthage
376431Inside of your ` Cartfile ` , add GitHub path to ` XMLCoder ` :
377432
378433``` ogdl
379- github "MaxDesiatov/XMLCoder" ~> 0.10 .0
434+ github "MaxDesiatov/XMLCoder" ~> 0.11 .0
380435```
381436
382437Then, run the following command to build the framework:
@@ -403,10 +458,10 @@ appreciated and helps in maintaining the project.
403458
404459### Coding Style
405460
406- This project uses [ SwiftFormat] ( https://github.com/nicklockwood/SwiftFormat )
461+ This project uses [ SwiftFormat] ( https://github.com/nicklockwood/SwiftFormat )
407462and [ SwiftLint] ( https://github.com/realm/SwiftLint ) to
408- enforce formatting and coding style. We encourage you to run SwiftFormat within
409- a local clone of the repository in whatever way works best for you either
463+ enforce formatting and coding style. We encourage you to run SwiftFormat within
464+ a local clone of the repository in whatever way works best for you either
410465manually or automatically via an [ Xcode
411466extension] ( https://github.com/nicklockwood/SwiftFormat#xcode-source-editor-extension ) ,
412467[ build phase] ( https://github.com/nicklockwood/SwiftFormat#xcode-build-phase ) or
@@ -424,8 +479,8 @@ pre-commit install # installs pre-commit hook to run checks before you commit
424479Refer to [ the pre-commit documentation page] ( https://pre-commit.com/ ) for more details
425480and installation instructions for other platforms.
426481
427- SwiftFormat and SwiftLint also run on CI for every PR and thus a CI build can
428- fail with incosistent formatting or style. We require CI builds to pass for all
482+ SwiftFormat and SwiftLint also run on CI for every PR and thus a CI build can
483+ fail with incosistent formatting or style. We require CI builds to pass for all
429484PRs before merging.
430485
431486### Test Coverage
0 commit comments