Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Playground - noun: a place where people can play
- import Foundation
- enum XMLReaderOptions: UInt {
- case ZeroValue = 0
- case ProcessNamespaces = 1
- case ReportNamespacePrefixes = 2
- case ResolveExternalEntities = 4
- }
- let kXMLReaderTextNodeKey: NSString = "text"
- let kXMLReaderAttributePrefix: NSString = "@"
- class XMLReader: NSObject {
- var dictionaryStack: NSMutableArray?
- var textInProgress: NSMutableString?
- var errorPointer: NSError
- func dictionaryForXMLData(data: NSData, error: NSError) -> NSDictionary {
- var reader: XMLReader = XMLReader(error: errorPointer)
- var rootDictionary: NSDictionary = reader.objectWithData(data, options: XMLReaderOptions.ZeroValue)
- return rootDictionary
- }
- func dictionaryForXMLString(string: NSString, error: NSError) -> NSDictionary {
- var data: NSData = string.dataUsingEncoding(NSUTF8StringEncoding)!
- return dictionaryForXMLData(data, error: errorPointer)
- }
- func dictionaryForXMLData(data: NSData, options: XMLReaderOptions, error: NSError) -> NSDictionary {
- var reader: XMLReader = XMLReader(error: errorPointer)
- var rootDictionary: NSDictionary = reader.objectWithData(data, options: options)
- return rootDictionary
- }
- func dictionaryForXMLString(string: NSString, options: XMLReaderOptions, error: NSError) -> NSDictionary {
- var data: NSData(string.dataUsingEncoding(NSUTF8StringEncoding))
- // return XMLReader.dictionaryForXMLData(data)
- }
- func initWithError(error: NSError) -> AnyObject {
- errorPointer = error
- }
- func objectWithData(data: NSData, options: XMLReaderOptions) -> NSDictionary {
- // Clear out any old Data
- dictionaryStack = NSMutableArray()
- textInProgress = NSMutableString()
- // Initialize the stack with a fresh dictionary
- dictionaryStack?.addObject(NSMutableDictionary())
- var parser: NSXMLParser = NSXMLParser(data: data)
- // debugging
- let options = XMLReaderOptions.ZeroValue
- let parser = Parse()
- parser.shouldProcessNamespaces(Bool(0 != options.rawValue & XMLReaderOptions.ProcessNamespaces.rawValue))
- // parser.shouldProcessNamespaces // (options & XMLReaderOptions.ProcessNamespaces)
- parser.shouldReportNamespacePrefixes // (options & XMLReaderOptionsReportNamespacePrefixes)
- parser.shouldResolveExternalEntities // (options & XMLReaderOptionsResolveExternalEntities)
- parser.delegate = self as? NSXMLParserDelegate
- var success: Bool = parser.parse()
- if success {
- var resultDict: NSDictionary = dictionaryStack?.objectAtIndex(0)
- return resultDict
- }
- return nil
- }
- func parser(parser: NSXMLParser, elementName: NSString, namespaceURI: NSString, qualifiedName: NSString, attributeDict: NSDictionary) {
- // Get the dictionary for the current level in the stack.
- var parentDict: NSMutableDictionary = dictionaryStack?.lastObject as NSMutableDictionary
- // Create the child dictionary for the new element.
- // and initialize it with the attributes.
- var childDict: NSMutableDictionary = NSMutableDictionary()
- childDict.addEntriesFromDictionary(attributeDict)
- var existingValue: AnyObject = parentDict.objectForKey(elementName)!
- if existingValue {
- var array: NSMutableArray = nil
- if existingValue.isKindOfClass(NSMutableArray()) {
- // The array exists, so use it!
- var array: NSMutableArray = existingValue
- } else {
- // W.I.P
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement