Skip to content

Instantly share code, notes, and snippets.

@sketchytech
Last active August 29, 2015 14:05
Show Gist options
  • Save sketchytech/549bdf3cceee00e06f72 to your computer and use it in GitHub Desktop.
Save sketchytech/549bdf3cceee00e06f72 to your computer and use it in GitHub Desktop.
This is just for fun and only currently works with very simple xml, no attributes
// implementation example (this is just for fun and only currently works with very simple xml, no attributes)
class MyDelegate:XMLDelegateClass {
func tagClosed(string:String) {
string
}
func tagOpen (string:String) {
string
}
func stringFound (string:String) {
string
}
var str = "<xml><title>Rough as Hell XML</title><intro>Some stuff</intro><body>Some more stuff about the stuff.</body><conclusion>Finishing up.</conclusion></xml>"
init () {
var a = XMLClass()
a.roughAndReadyXMLParse(str,delegate: self)}
}
protocol XMLDelegateClass {
func tagClosed(string:String)
func tagOpen (string:String)
func stringFound (string:String)
}
class XMLClass {
func roughAndReadyXMLParse(string:String, delegate:XMLDelegateClass) {
var strPlace = ""
var tagFlag = false
var tagClosing = false
var tagOpening = false
for i in string {
if tagFlag && tagClosing && i == ">" {
tagFlag = false
tagClosing = false
delegate.tagClosed(dropFirst(strPlace))
strPlace = ""
}
if tagFlag && tagOpening && i == ">" {
tagFlag = false
tagOpening = false
delegate.tagOpen(strPlace)
strPlace = ""
}
if tagFlag && tagOpening {
strPlace.append(i)
}
if tagFlag && tagClosing && !tagOpening {
strPlace.append(i)
}
if i == "<" {
tagFlag = true
if strPlace.isEmpty == false {
delegate.stringFound(dropFirst(strPlace))
}
strPlace = ""
}
if tagFlag && i == "/" {
tagClosing = true
}
if tagFlag && !tagClosing {
tagOpening = true
}
// Retrieve Strings
if !tagFlag && !tagClosing && !tagOpening {
strPlace.append(i)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment