Last active
August 29, 2015 14:05
-
-
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
This file contains 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
// 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)} | |
} |
This file contains 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
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