diff --git a/README.md b/README.md
index ce8efb3..0c16e1d 100755
--- a/README.md
+++ b/README.md
@@ -119,7 +119,7 @@ let str = """
"""
-xml = try! XML.parse(string) // -> XML.Accessor
+xml = try! XML.parse(str) // -> XML.Accessor
```
+ from NSData
```swift
@@ -136,11 +136,27 @@ let str = """
"""
-let data = string.dataUsingEncoding(NSUTF8StringEncoding)
+let data = str.data(using: .utf8)
xml = XML.parse(data) // -> XML.Accessor
```
++ with invalid character
+
+```swift
+let srt = "@ß123\u{1c}"
+
+let xml = XML.parse(str.data(using: .utf8))
+
+if case .failure(XMLError.intrupptedParseError) = xml {
+ print("invalid character")
+}
+
+```
+
+For more, see https://developer.apple.com/documentation/foundation/xmlparser/errorcode
+
+
### 2. Access child Elements
```swift
let element = xml.ResultSet // -> XML.Accessor
diff --git a/SwiftyXMLParser/Error.swift b/SwiftyXMLParser/Error.swift
index 3717b7d..0f667b4 100755
--- a/SwiftyXMLParser/Error.swift
+++ b/SwiftyXMLParser/Error.swift
@@ -25,6 +25,7 @@
import Foundation
public enum XMLError: Error {
- case parseError
+ case failToEncodeString
+ case intrupptedParseError(rawError: Error)
case accessError(description: String)
}
diff --git a/SwiftyXMLParser/Parser.swift b/SwiftyXMLParser/Parser.swift
index 47c0e12..ed1fc6f 100755
--- a/SwiftyXMLParser/Parser.swift
+++ b/SwiftyXMLParser/Parser.swift
@@ -26,13 +26,22 @@ import Foundation
extension XML {
class Parser: NSObject, XMLParserDelegate {
+ /// If it has value, Parser is interuppted by error. (e.g. using invalid character)
+ /// So the result of parsing is missing.
+ /// See https://developer.apple.com/documentation/foundation/xmlparser/errorcode
+ private(set) var error: XMLError?
+
func parse(_ data: Data) -> Accessor {
stack = [Element]()
stack.append(documentRoot)
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
- return Accessor(documentRoot)
+ if let error = error {
+ return Accessor(error)
+ } else {
+ return Accessor(documentRoot)
+ }
}
override init() {
@@ -75,5 +84,9 @@ extension XML {
}
stack.removeLast()
}
+
+ func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
+ error = .intrupptedParseError(rawError: parseError)
+ }
}
}
diff --git a/SwiftyXMLParser/XML.swift b/SwiftyXMLParser/XML.swift
index 79820ca..94b2988 100755
--- a/SwiftyXMLParser/XML.swift
+++ b/SwiftyXMLParser/XML.swift
@@ -104,7 +104,7 @@ open class XML {
*/
open class func parse(_ str: String) throws -> Accessor {
guard let data = str.data(using: String.Encoding.utf8) else {
- throw XMLError.parseError
+ throw XMLError.failToEncodeString
}
return Parser().parse(data)
@@ -130,7 +130,7 @@ open class XML {
*/
open class func parse(_ str: String, trimming manner: CharacterSet) throws -> Accessor {
guard let data = str.data(using: String.Encoding.utf8) else {
- throw XMLError.parseError
+ throw XMLError.failToEncodeString
}
return Parser(trimming: manner).parse(data)
diff --git a/SwiftyXMLParserTests/ParserTests.swift b/SwiftyXMLParserTests/ParserTests.swift
index bd974dd..b0fdcb7 100755
--- a/SwiftyXMLParserTests/ParserTests.swift
+++ b/SwiftyXMLParserTests/ParserTests.swift
@@ -61,10 +61,10 @@ class ParserTests: XCTestCase {
}
let xml = XML.Parser().parse(data)
- if let _ = xml["ResultSet"].error {
- XCTFail("fail to parse")
+ if case .failure(XMLError.intrupptedParseError) = xml {
+ XCTAssert(true, "Parsed Failure because of the invalid character")
} else {
- XCTAssert(true, "success to parse")
+ XCTAssert(false, "fail")
}
}
@@ -142,5 +142,22 @@ class ParserTests: XCTestCase {
} else {
XCTAssert(false, "fail")
}
- }
+ }
+
+ func testParseErrorToInvalidCharacter() {
+ let str = "@ß123\u{1c}"
+ let xml = XML.Parser().parse(str.data(using: .utf8)!)
+
+ if case .failure(XMLError.intrupptedParseError) = xml {
+ XCTAssert(true, "Parsed Failure because of the invalid character")
+ } else {
+ XCTAssert(false, "fail")
+ }
+ }
+
+ func testNotParseErrorToInvalidCharacter() {
+ let str = "@ß123\u{1c}".addingPercentEncoding(withAllowedCharacters: CharacterSet.controlCharacters.inverted)!
+ let xml = XML.Parser().parse(str.data(using: .utf8)!)
+ XCTAssertEqual("@ß123\u{1c}", xml["xmlopening"].text?.removingPercentEncoding, "Parsed Success and trim them")
+ }
}
diff --git a/SwiftyXMLParserTests/XMLTests.swift b/SwiftyXMLParserTests/XMLTests.swift
index 67ce9f9..30ff386 100755
--- a/SwiftyXMLParserTests/XMLTests.swift
+++ b/SwiftyXMLParserTests/XMLTests.swift
@@ -70,7 +70,7 @@ class XMLTests: XCTestCase {
func testSuccessParseFromDoublebyteSpace() {
- guard let xml = try? XML.parse(" ") else {
+ guard let xml = try? XML.parse(" ") else {
XCTFail("Fail Prase")
return
}