Skip to content

Commit

Permalink
Made XmlElement.text non-optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Oct 28, 2017
1 parent 2e278fa commit 60a7d4f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/XmlElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
open class XmlElement {
open var name: String
open var attributes: [String:String] = [:]
open var text: String?
open var text: String = "" //I guess this is ok.
open var children: [XmlElement] = []
open var parent: XmlElement?

Expand All @@ -24,7 +24,7 @@ open class XmlElement {

open var xml: String {
//TODO: generate compact xml
return "<\(name)\(attributes.map({" \($0)=\"\($1)\" "}).joined())>\(text ?? "")\(children.map({ $0.description }).joined())</\(name)>"
return "<\(name)\(attributes.map({" \($0)=\"\($1)\" "}).joined())>\(text)\(children.map({ $0.description }).joined())</\(name)>"
}
}

Expand Down
8 changes: 2 additions & 6 deletions Sources/XmlParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,12 @@ extension XmlParser: XMLParserDelegate {
}

public func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElement.text == nil {
currentElement.text = string
} else {
currentElement.text! += string
}
currentElement.text += string
}

public func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
assert(currentElement.name == elementName, "This should not ever happen")
currentElement.text = currentElement.text?.trimmingCharacters(in: .whitespacesAndNewlines)
currentElement.text = currentElement.text.trimmingCharacters(in: .whitespacesAndNewlines)
currentElement = currentElement.parent
self.delegate?.parser(self, didEndElement: elementName)
}
Expand Down

0 comments on commit 60a7d4f

Please sign in to comment.