Skip to content

Commit

Permalink
Merge pull request #119 from Arcikbtw/develop
Browse files Browse the repository at this point in the history
Enable different encodings
  • Loading branch information
drmohundro authored Aug 26, 2017
2 parents 93f449d + cc4b25d commit be482f0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Source/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class SWXMLHashOptions {
/// Matching element names, element values, attribute names, attribute values
/// will be case insensitive. This will not affect parsing (data does not change)
public var caseInsensitive = false

/// Encoding used for XML parsing. Default is set to UTF8
public var encoding = String.Encoding.utf8
}

/// Simple XML parser
Expand Down Expand Up @@ -80,7 +83,10 @@ public class SWXMLHash {
- returns: an `XMLIndexer` instance that can be iterated over
*/
public func parse(_ xml: String) -> XMLIndexer {
return parse(xml.data(using: String.Encoding.utf8)!)
guard let data = xml.data(using: options.encoding) else {
return .xmlError(.encoding)
}
return parse(data)
}

/**
Expand Down Expand Up @@ -444,6 +450,7 @@ public enum IndexingError: Error {
case key(key: String)
case index(idx: Int)
case initialize(instance: AnyObject)
case encoding
case error

// swiftlint:disable identifier_name
Expand Down Expand Up @@ -734,6 +741,8 @@ extension IndexingError: CustomStringConvertible {
return "XML Element Error: Incorrect index [\"\(index)\"]"
case .initialize(let instance):
return "XML Indexer Error: initialization with Object [\"\(instance)\"]"
case .encoding:
return "String Encoding Error"
case .error:
return "Unknown Error"
}
Expand Down

0 comments on commit be482f0

Please sign in to comment.