Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@drmohundro
Created February 20, 2015 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmohundro/ad39a95d917fc74c51f5 to your computer and use it in GitHub Desktop.
Save drmohundro/ad39a95d917fc74c51f5 to your computer and use it in GitHub Desktop.
SWXMLHash Lazy-Loading Approach
class IndexOp {
let index: Int
let key: String
init(_ index: Int) {
self.index = index
self.key = ""
}
init(_ key: String) {
self.key = key
self.index = -1
}
func toString() -> String {
if key == "" {
return index.description
}
return key
}
}
class IndexOps {
var ops: [IndexOp] = []
func stringify() -> String {
var s = ""
for op in ops {
s += "[" + op.toString() + "]"
}
return s
}
}
enum XMLIndexer {
case Stream(IndexOps)
var element: String {
get {
switch self {
case .Stream(let ops):
return ops.stringify()
}
}
}
init(_ rawObject: AnyObject) {
self = .Stream(IndexOps())
}
subscript(key: String) -> XMLIndexer {
get {
switch self {
case .Stream(let elem):
let op = IndexOp(key)
elem.ops.append(op)
return .Stream(elem)
}
}
}
subscript(index: Int) -> XMLIndexer {
get {
switch self {
case .Stream(let elem):
let op = IndexOp(index)
elem.ops.append(op)
return .Stream(elem)
}
}
}
}
let f = XMLIndexer("test")
// outputs "[test][1][test2]"
f["test"][1]["test2"].element
@drmohundro
Copy link
Author

My current thinking at the moment is that, when element is called, it would use the IndexOps list to then parse the XML. It will also need to support withAttr for attribute support.

I'd imagine that calling children or all would result in parsing down to that level (just as calling element would), but that might be solved by just letting element do the parsing and children/all would delegate to children.

@drmohundro
Copy link
Author

@tkrajacic
Copy link

To be honest, I do not yet see exactly how this is supposed to work. Then again, I am a mechanical engineer, not a computer scientist 😀
It for sure is a novel approach for me!

So, you parse the xml and only build these 'stringified' paths right? And only when something is requested, you get the actual information from the xml file.
Or am I completely wrong here...?
If this is indeed the case, then I guess a trie structure would be the right choice to store the path components?
Meh I guess I have studied the wrong thing ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment