Feature: Set text, append element and ignore namespaces#64
Merged
kazuhiro4949 merged 13 commits intoyahoojapan:masterfrom Sep 20, 2021
Merged
Feature: Set text, append element and ignore namespaces#64kazuhiro4949 merged 13 commits intoyahoojapan:masterfrom
kazuhiro4949 merged 13 commits intoyahoojapan:masterfrom
Conversation
SwiftyXMLParser/XML.swift
Outdated
| Global XML setting for ignoring namespaces. | ||
| If set to true all accessors will ignore the first part of an element name up to a semicolon (:). | ||
| */ | ||
| public static var ignoreNamespaces = false |
Member
There was a problem hiding this comment.
ignoreNamespaces should be a local property. Could you place it on Element class?
extension XML {
open class Element {
open var name: String
open var text: String?
open var attributes = [String: String]()
open var childElements = [Element]()
open var lineNumberStart = -1
open var lineNumberEnd = -1
open var CDATA: Data?
open var ignoreNamespaces: Bool // <-
public init(name: String, ignoreNamespaces: Bool = false, text: String? = nil, attributes: [String: String] = [:], childElements: [Element] = []) {and set the boolean value in Parser class
extension XML {
class Parser: NSObject, XMLParserDelegate {
// ...
init(ignoreNamespaces: Bool = false) {
self.ignoreNamespaces = ignoreNamespaces
trimmingManner = nil
documentRoot = Element(name: "XML.Parser.AbstructedDocumentRoot", ignoreNamespaces: ignoreNamespaces)
}
init(trimming manner: CharacterSet, ignoreNamespaces: Bool = false) {
self.ignoreNamespaces = ignoreNamespaces
trimmingManner = manner
documentRoot = Element(name: "XML.Parser.AbstructedDocumentRoot", ignoreNamespaces: ignoreNamespaces)
}
// MARK:- private
fileprivate var documentRoot: Element
fileprivate var stack = [Element]()
fileprivate let trimmingManner: CharacterSet?
fileprivate let ignoreNamespaces: Bool // <-
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
let node = Element(name: elementName, ignoreNamespaces: ignoreNamespaces) // <-change XML I/F.
open class XML {
// /**
// Global XML setting for ignoring namespaces.
// If set to true all accessors will ignore the first part of an element name up to a semicolon (:).
// */
// public static var ignoreNamespaces = false
/**
Interface to parse NSData
- parameter data:NSData XML document
- returns:Accessor object to access XML document
*/
open class func parse(_ data: Data, ignoreNamespaces: Bool = false) -> Accessor {
return Parser(ignoreNamespaces: ignoreNamespaces).parse(data)
}
/**
Interface to parse String
- Parameter str:String XML document
- Returns:Accessor object to access XML document
*/
open class func parse(_ str: String, ignoreNamespaces: Bool = false) throws -> Accessor { // <-
guard let data = str.data(using: String.Encoding.utf8) else {
throw XMLError.failToEncodeString
}
return Parser(ignoreNamespaces: ignoreNamespaces).parse(data)
}
/**
Interface to parse NSData
- parameter data:NSData XML document
- parameter manner:NSCharacterSet If you wannna trim Text, assign this arg
- returns:Accessor object to access XML document
*/
open class func parse(_ data: Data, trimming manner: CharacterSet, ignoreNamespaces: Bool = false) -> Accessor { // <-
return Parser(trimming: manner, ignoreNamespaces: ignoreNamespaces).parse(data)
}
/**
Interface to parse String
- Parameter str:String XML document
- parameter manner:NSCharacterSet If you wannna trim Text, assign this arg
- Returns:Accessor object to access XML document
*/
open class func parse(_ str: String, trimming manner: CharacterSet, ignoreNamespaces: Bool = false) throws -> Accessor { // <-
guard let data = str.data(using: String.Encoding.utf8) else {
throw XMLError.failToEncodeString
}
return Parser(trimming: manner, ignoreNamespaces: ignoreNamespaces).parse(data)
}
Member
|
@mrotrifork We appreciate your contribution. |
…leanup initialisers
Contributor
Author
|
Good feedback. I moved ignoreNamespaces to live inside XML.Element which can be set via parse(). I also cleaned up some of the initialisers. |
kazuhiro4949
approved these changes
Sep 20, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR covers the following 3 features. Might have been better to split it in 3 pull requests, but let us see if we can go through them one by one and I will make the necessary adjustments:
Feature 1: Ability to get and set text and attributes on single element using the Accessor, and keep the modified elements when creating an xml document. The same small change could also be applied to other types than text/string. It requires that the accessor is not created as a constant.
Feature 2: Ability to append a new element to an accessor.
Feature 3: Global flag for ignoring namespaces when accessing elements. Currently I am using this framework with SOAP XML, which has dynamically prefixed namespaces on all elements.
Bugfix 1: XML documents are created with an extra space when no attributes are added for an element.
Bugfix 2: XML declaration prefix is not optional.
Bugfix 3: XML Element does not have a full initialiser.
All 3 features have corresponding unit tests. Let me know what you think.