-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLParserTbx.swift
executable file
·163 lines (104 loc) · 4.84 KB
/
XMLParserTbx.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// XMLParserTbx.swift
// Tinderbox-Aeon-Exchange
//
// Created by prenez on 6/28/15.
// Copyright © 2015 Michael Prenez-Isbell. All rights reserved.
//
import Foundation
import AppKit
import Cocoa
import AEXML
class XMLParserTbx {
var path: String
var data: Data
var items: [TbxItem]?
var tbxItems : TbxItem = TbxItem()
var itemIDNumbers : [Int] = []
init?(contentPath: URL) {
self.path = contentPath.path
self.data = FileManager.default.contents(atPath: path)!
}
func getChildren(parentItem: TbxItem, element : AEXMLElement) {
var currentItem = TbxItem()
for child in element.children {
if let _ = child.value {
// print("\(child.name) and \(child.value!)")
currentItem = TbxItem(name: child.name, value: child.value!, children: [])
parentItem.addChild(item: currentItem)
}
else {
// print("\(child.name) and NO VALUE )")
currentItem = TbxItem(name: child.name, value: "", children: [])
parentItem.addChild(item: currentItem)
}
for attribute in child.attributes {
if currentItem.name == "item" {
if attribute.0 == "ID" {
let x = Int(attribute.1 )
self.itemIDNumbers.append(x!)
}
}
currentItem.addChild(item: TbxItem(name: attribute.0 as! String, value: attribute.1 as! String, children: [])) // this could break
}
getChildren(parentItem: currentItem, element: child)
}
}
func parse() {
do {
let x = try AEXMLDocument(xml: self.data)
} catch {
print("\(error)")
}
do {
let xmlDoc = try AEXMLDocument(xml: self.data)
// might want to structure it
// parse items
// parse colors
// parse etc
// all the top level stuff
// also think about making an array for each of the top level elements,
// the easier to make key value lists with later
// var firstelement = xmlDoc.root["item"]
// print (xmlDoc.root[xmldoc "item"].stringValue)
// var firstitem = xmlDoc.root["attrib"].countWithAttributes(["Name" : "Item"])
// xmlDoc.root["attrib"].countWithAttributes(["Name" : "Item"])
// this parses every element in the document
// this is where i build my array
var currentItem = TbxItem(name: (xmlDoc.root.first?.name)!, value: "", children: [])
self.tbxItems = currentItem
for attribute in xmlDoc.root.attributes {
currentItem.addChild(item: TbxItem(name: attribute.0 as! String, value: attribute.1 as! String, children: [])) // this could break
}
for child in xmlDoc.root.children {
if let _ = child.value {
// print("\(child.name) and \(child.value!)")
currentItem = TbxItem(name: child.name, value: child.value!, children: [])
self.tbxItems.addChild(item: currentItem)
}
else {
//print("\(child.name) and NO VALUE NO VALUE NO VALUE )")
currentItem = TbxItem(name: child.name, value: "", children: [])
self.tbxItems.addChild(item: currentItem)
}
for attribute in child.attributes {
if currentItem.name == "item" {
if attribute.0 == "ID" {
let x = Int(attribute.1 as! String)
self.itemIDNumbers.append(x!)
}
}
currentItem.addChild(item: TbxItem(name: attribute.0 as! String, value: attribute.1 as! String, children: [])) // this could break
}
getChildren(parentItem: currentItem, element: child)
}
let sortedItemIDNumbers = itemIDNumbers.sorted()
let lowest = sortedItemIDNumbers.first
// make accessible to outlineview
self.items?.append(tbxItems)
}
catch {
print("\(error)")
}
}
}