From d864b9a245f61ceca89d44254b31d9f42b841339 Mon Sep 17 00:00:00 2001 From: Artur D Date: Thu, 19 Jan 2017 11:23:50 +0100 Subject: [PATCH 1/3] Enable different encodings --- Source/SWXMLHash.swift | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Source/SWXMLHash.swift b/Source/SWXMLHash.swift index 82b228f4..1ece9610 100644 --- a/Source/SWXMLHash.swift +++ b/Source/SWXMLHash.swift @@ -74,7 +74,20 @@ 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)!) + return parse(xml, using: String.Encoding.utf8) + } + + /** + Begins parsing the passed in XML string. + + - parameters: + - xml: an XML string. __Note__ that this is not a URL but a + - encoding: Encoding use to parse xml + string containing XML. + - returns: an `XMLIndexer` instance that can be iterated over + */ + public func parse(_ xml: String, using encoding: String.Encoding) -> XMLIndexer { + return parse(xml.data(using: encoding)!) } /** @@ -100,6 +113,18 @@ public class SWXMLHash { class public func parse(_ xml: String) -> XMLIndexer { return SWXMLHash().parse(xml) } + + /** + Method to parse XML passed in as a string. + + - parameter + - xml: The XML to be parsed + - encoding: Encoding use to parse xml + - returns: An XMLIndexer instance that is used to look up elements in the XML + */ + class public func parse(_ xml: String, using encoding: String.Encoding) -> XMLIndexer { + return SWXMLHash().parse(xml, using: encoding) + } /** Method to parse XML passed in as a Data instance. From 2cf92e8c000a67190520300a81e3a78bbbc526d2 Mon Sep 17 00:00:00 2001 From: Artur D Date: Wed, 23 Aug 2017 10:19:29 +0200 Subject: [PATCH 2/3] Update config object to handle different encodings --- Source/SWXMLHash.swift | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/Source/SWXMLHash.swift b/Source/SWXMLHash.swift index c7773858..0afde367 100644 --- a/Source/SWXMLHash.swift +++ b/Source/SWXMLHash.swift @@ -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 @@ -80,22 +83,12 @@ public class SWXMLHash { - returns: an `XMLIndexer` instance that can be iterated over */ public func parse(_ xml: String) -> XMLIndexer { - return parse(xml, using: String.Encoding.utf8) + guard let data = xml.data(using: options.encoding) else { + return .xmlError(.encoding) + } + return parse(data) } - /** - Begins parsing the passed in XML string. - - - parameters: - - xml: an XML string. __Note__ that this is not a URL but a - - encoding: Encoding use to parse xml - string containing XML. - - returns: an `XMLIndexer` instance that can be iterated over - */ - public func parse(_ xml: String, using encoding: String.Encoding) -> XMLIndexer { - return parse(xml.data(using: encoding)!) - } - /** Begins parsing the passed in XML string. @@ -119,18 +112,6 @@ public class SWXMLHash { class public func parse(_ xml: String) -> XMLIndexer { return SWXMLHash().parse(xml) } - - /** - Method to parse XML passed in as a string. - - - parameter - - xml: The XML to be parsed - - encoding: Encoding use to parse xml - - returns: An XMLIndexer instance that is used to look up elements in the XML - */ - class public func parse(_ xml: String, using encoding: String.Encoding) -> XMLIndexer { - return SWXMLHash().parse(xml, using: encoding) - } /** Method to parse XML passed in as a Data instance. @@ -469,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 @@ -759,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" } From cc4b25da366e47c3ffb0b7619b6a3babcb8dffc4 Mon Sep 17 00:00:00 2001 From: David Mohundro Date: Sat, 26 Aug 2017 08:01:42 -0500 Subject: [PATCH 3/3] Remove whitespace to make swiftlint happy --- Source/SWXMLHash.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/SWXMLHash.swift b/Source/SWXMLHash.swift index 0afde367..c947818c 100644 --- a/Source/SWXMLHash.swift +++ b/Source/SWXMLHash.swift @@ -47,7 +47,7 @@ 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 } @@ -88,7 +88,7 @@ public class SWXMLHash { } return parse(data) } - + /** Begins parsing the passed in XML string.