diff --git a/SwiftyXMLParser/Parser.swift b/SwiftyXMLParser/Parser.swift index f23f883..3104947 100755 --- a/SwiftyXMLParser/Parser.swift +++ b/SwiftyXMLParser/Parser.swift @@ -33,11 +33,25 @@ extension XML { /// 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 { + + private func setupParsing() { stack = [Element]() stack.append(documentRoot) + } + + func parse(_ data: Data) -> Accessor { + setupParsing() let parser = XMLParser(data: data) + return startParsing(parser) + } + + func parse(_ stream: InputStream) -> Accessor { + setupParsing() + let parser = XMLParser(stream: stream) + return startParsing(parser) + } + + private func startParsing(_ parser: XMLParser) -> Accessor { parser.delegate = self parser.parse() if let error = error { diff --git a/SwiftyXMLParser/XML.swift b/SwiftyXMLParser/XML.swift index bc201dc..28edd9a 100755 --- a/SwiftyXMLParser/XML.swift +++ b/SwiftyXMLParser/XML.swift @@ -88,7 +88,28 @@ public func ?<< (lhs: inout [T], rhs: T?) { open class XML { /** - Interface to parse Data + Interface to parse InputStream + + - parameter strea:InputStream for an XML document + - returns:Accessor object to access XML document + */ + open class func parse(_ stream: InputStream) -> Accessor { + return Parser().parse(stream) + } + + + /** + Interface to parse NSData + + - parameter data:NSData XML document + - returns:Accessor object to access XML document + */ + open class func parse(_ data: Data) -> Accessor { + return Parser().parse(data) + } + + /** + Interface to parse String - parameter data:Data XML document - parameter manner:CharacterSet If you want to trim text (default off)