Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. // Playground - noun: a place where people can play
  2.  
  3. import Foundation
  4.  
  5. enum XMLReaderOptions: UInt {
  6. case ZeroValue = 0
  7. case ProcessNamespaces = 1
  8. case ReportNamespacePrefixes = 2
  9. case ResolveExternalEntities = 4
  10. }
  11.  
  12. let kXMLReaderTextNodeKey: NSString = "text"
  13. let kXMLReaderAttributePrefix: NSString = "@"
  14.  
  15. class XMLReader: NSObject {
  16.  
  17. var dictionaryStack: NSMutableArray?
  18. var textInProgress: NSMutableString?
  19. var errorPointer: NSError
  20.  
  21. func dictionaryForXMLData(data: NSData, error: NSError) -> NSDictionary {
  22.  
  23. var reader: XMLReader = XMLReader(error: errorPointer)
  24. var rootDictionary: NSDictionary = reader.objectWithData(data, options: XMLReaderOptions.ZeroValue)
  25. return rootDictionary
  26.  
  27. }
  28.  
  29. func dictionaryForXMLString(string: NSString, error: NSError) -> NSDictionary {
  30. var data: NSData = string.dataUsingEncoding(NSUTF8StringEncoding)!
  31. return dictionaryForXMLData(data, error: errorPointer)
  32. }
  33.  
  34. func dictionaryForXMLData(data: NSData, options: XMLReaderOptions, error: NSError) -> NSDictionary {
  35.  
  36. var reader: XMLReader = XMLReader(error: errorPointer)
  37. var rootDictionary: NSDictionary = reader.objectWithData(data, options: options)
  38. return rootDictionary
  39.  
  40. }
  41.  
  42. func dictionaryForXMLString(string: NSString, options: XMLReaderOptions, error: NSError) -> NSDictionary {
  43.  
  44. var data: NSData(string.dataUsingEncoding(NSUTF8StringEncoding))
  45. // return XMLReader.dictionaryForXMLData(data)
  46.  
  47. }
  48.  
  49. func initWithError(error: NSError) -> AnyObject {
  50. errorPointer = error
  51. }
  52.  
  53. func objectWithData(data: NSData, options: XMLReaderOptions) -> NSDictionary {
  54.  
  55. // Clear out any old Data
  56. dictionaryStack = NSMutableArray()
  57. textInProgress = NSMutableString()
  58.  
  59. // Initialize the stack with a fresh dictionary
  60. dictionaryStack?.addObject(NSMutableDictionary())
  61. var parser: NSXMLParser = NSXMLParser(data: data)
  62.  
  63. // debugging
  64. let options = XMLReaderOptions.ZeroValue
  65. let parser = Parse()
  66.  
  67. parser.shouldProcessNamespaces(Bool(0 != options.rawValue & XMLReaderOptions.ProcessNamespaces.rawValue))
  68.  
  69.  
  70.  
  71. // parser.shouldProcessNamespaces // (options & XMLReaderOptions.ProcessNamespaces)
  72. parser.shouldReportNamespacePrefixes // (options & XMLReaderOptionsReportNamespacePrefixes)
  73. parser.shouldResolveExternalEntities // (options & XMLReaderOptionsResolveExternalEntities)
  74. parser.delegate = self as? NSXMLParserDelegate
  75.  
  76. var success: Bool = parser.parse()
  77. if success {
  78. var resultDict: NSDictionary = dictionaryStack?.objectAtIndex(0)
  79. return resultDict
  80. }
  81.  
  82. return nil
  83. }
  84.  
  85. func parser(parser: NSXMLParser, elementName: NSString, namespaceURI: NSString, qualifiedName: NSString, attributeDict: NSDictionary) {
  86.  
  87. // Get the dictionary for the current level in the stack.
  88. var parentDict: NSMutableDictionary = dictionaryStack?.lastObject as NSMutableDictionary
  89.  
  90. // Create the child dictionary for the new element.
  91. // and initialize it with the attributes.
  92. var childDict: NSMutableDictionary = NSMutableDictionary()
  93. childDict.addEntriesFromDictionary(attributeDict)
  94.  
  95. var existingValue: AnyObject = parentDict.objectForKey(elementName)!
  96. if existingValue {
  97.  
  98. var array: NSMutableArray = nil
  99. if existingValue.isKindOfClass(NSMutableArray()) {
  100.  
  101. // The array exists, so use it!
  102. var array: NSMutableArray = existingValue
  103.  
  104. } else {
  105.  
  106. // W.I.P
  107.  
  108. }
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement