-
Notifications
You must be signed in to change notification settings - Fork 0
/
XML.swift
70 lines (60 loc) · 2.31 KB
/
XML.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// ByvLocalizableStringsGenerator.swift
// ByvLocalizableStringsGenerator
//
// Created by Adrian Apodaca on 3/5/18.
// Copyright © 2017 byvapps. All rights reserved.
//
import Foundation
class XML: NSObject {
var parser: XMLParser?
var completion:([[String: String]]) -> Void
var searchParams:[String]
var items:[[String: String]] = [];
var foundCharacters:String? = nil;
var searchingValue: String? = nil
required init(data: Data, searchParams:[String], completion: @escaping ([[String: String]]) -> Void) {
self.searchParams = searchParams
self.completion = completion
super.init()
self.parser = XMLParser(data: data)
self.parser?.delegate = self;
if !(self.parser?.parse() ?? false) {
completion([])
print("Error parsing XML")
}
}
}
extension XML: XMLParserDelegate {
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String: String]) {
if elementName == "userDefinedRuntimeAttribute" {
if let keyPath = attributeDict["keyPath"], searchParams.contains(keyPath) {
if let string = attributeDict["value"] {
items.append(["keyPath": keyPath,
"value": string])
} else {
searchingValue = keyPath
}
}
} else if searchingValue != nil && elementName == "string" && (attributeDict["key"] == "value") {
foundCharacters = ""
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if let chars = foundCharacters {
self.foundCharacters = chars + string;
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if let keyPath = searchingValue, let string = foundCharacters, elementName == "string" {
items.append(["keyPath": keyPath,
"value": string])
foundCharacters = nil
searchingValue = nil
}
}
func parserDidEndDocument(_ parser: XMLParser) {
print("XML items: \(items)")
completion(items)
}
}