Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream support #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions SwiftyXMLParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 22 additions & 1 deletion SwiftyXMLParser/XML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,28 @@ public func ?<< <T>(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)
Expand Down