Skip to content

Commit

Permalink
Merge pull request #89 from gca3020/master
Browse files Browse the repository at this point in the history
Automatic Deserialization of XMLAttribute parameters.
  • Loading branch information
drmohundro authored Aug 8, 2016
2 parents 2527ffe + 896da04 commit a408118
Show file tree
Hide file tree
Showing 11 changed files with 559 additions and 62 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Given:
The below will return "123".

```swift
xml["root"]["catalog"]["book"][1].element?.attributes["id"]
xml["root"]["catalog"]["book"][1].element?.attribute(by: "id")?.text
```

Alternatively, you can look up an element with specific attributes. The below will return "John".
Expand Down Expand Up @@ -290,17 +290,17 @@ Given:
```xml
<root>
<books>
<book>
<book isbn="0000000001">
<title>Book A</title>
<price>12.5</price>
<year>2015</year>
</book>
<book>
<book isbn="0000000002">
<title>Book B</title>
<price>10</price>
<year>1988</year>
</book>
<book>
<book isbn="0000000003">
<title>Book C</title>
<price>8.33</price>
<year>1990</year>
Expand All @@ -317,13 +317,15 @@ struct Book: XMLIndexerDeserializable {
let price: Double
let year: Int
let amount: Int?
let isbn: Int

static func deserialize(node: XMLIndexer) throws -> Book {
return try Book(
title: node["title"].value(),
price: node["price"].value(),
year: node["year"].value(),
amount: node["amount"].value()
amount: node["amount"].value(),
isbn: node.value(ofAttribute: "isbn")
)
}
}
Expand All @@ -337,9 +339,11 @@ let books: [Book] = try xml["root"]["books"]["book"].value()

<img src="https://raw.githubusercontent.com/ncreated/SWXMLHash/assets/types-conversion%402x.png" width="600" alt="Types Conversion" />

Built-in, leaf-nodes converters support `Int`, `Double`, `Float`, `Bool`, and `String` values (both non- and -optional variants). Custom converters can be added by implementing `XMLElementDeserializable`.
You can convert any XML to your custom type by implementing `XMLIndexerDeserializable` for any non-leaf node (e.g. `<book>` in the example above).

For leaf nodes (e.g. `<title>` in the example above), built-in converters support `Int`, `Double`, `Float`, `Bool`, and `String` values (both non- and -optional variants). Custom converters can be added by implementing `XMLElementDeserializable`.

You can convert any XML to your custom type by implementing `XMLIndexerDeserializable`.
For attributes (e.g. `isbn=` in the example above), built-in converters support the same types as above, and additional converters can be added by implementing `XMLAttributeDeserializable`.

Types conversion supports error handling, optionals and arrays. For more examples, look into `SWXMLHashTests.swift` or play with types conversion directly in the Swift playground.

Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Loading

0 comments on commit a408118

Please sign in to comment.