Mac OS X中的Xml解析

时间:2014-11-04 06:54:44

标签: xml macos swift osx-yosemite

class XMLParserDelegate :NSObject , NSXMLParserDelegate {

    // Document handling methods
    func parserDidStartDocument(parser: NSXMLParser!){
        Console.Debug("parsing tiapp.xml started...")
    }

    // sent when the parser begins parsing of the document.
    func parserDidEndDocument(parser: NSXMLParser!){
        Console.Debug("parsing tiapp.xml ended...")
    }

    // sent when the parser has completed parsing. If this is encountered, the parse was successful.
    // DTD handling methods for various declarations.
    func parser(parser: NSXMLParser!, foundNotationDeclarationWithName name: String!, publicID: String!, systemID: String!){

    }

    func parser(parser: NSXMLParser!, foundUnparsedEntityDeclarationWithName name: String!, publicID: String!, systemID: String!, notationName: String!){

    }

    func parser(parser: NSXMLParser!, foundAttributeDeclarationWithName attributeName: String!, forElement elementName: String!, type: String!, defaultValue: String!){

    }

    func parser(parser: NSXMLParser!, foundElementDeclarationWithName elementName: String!, model: String!){

    }

    func parser(parser: NSXMLParser!, foundInternalEntityDeclarationWithName name: String!, value: String!){

    }

    func parser(parser: NSXMLParser!, foundExternalEntityDeclarationWithName name: String!, publicID: String!, systemID: String!){

    }

    func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!){

    }

    // sent when the parser finds an element start tag.
    // In the case of the cvslog tag, the following is what the delegate receives:
    //   elementName == cvslog, namespaceURI == http://xml.apple.com/cvslog, qualifiedName == cvslog
    // In the case of the radar tag, the following is what's passed in:
    //    elementName == radar, namespaceURI == http://xml.apple.com/radar, qualifiedName == radar:radar
    // If namespace processing >isn't< on, the xmlns:radar="http://xml.apple.com/radar" is returned as an attribute pair, the elementName is 'radar:radar' and there is no qualifiedName.
    func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!){

    }

    // sent when an end tag is encountered. The various parameters are supplied as above.    
    func parser(parser: NSXMLParser!, didStartMappingPrefix prefix: String!, toURI namespaceURI: String!){

    }

    // sent when the parser first sees a namespace attribute.
    // In the case of the cvslog tag, before the didStartElement:, you'd get one of these with prefix == @"" and namespaceURI == @"http://xml.apple.com/cvslog" (i.e. the default namespace)
    // In the case of the radar:radar tag, before the didStartElement: you'd get one of these with prefix == @"radar" and namespaceURI == @"http://xml.apple.com/radar"
    func parser(parser: NSXMLParser!, didEndMappingPrefix prefix: String!){

    }

    // sent when the namespace prefix in question goes out of scope.    
    func parser(parser: NSXMLParser!, foundCharacters string: String!){

    }

    // This returns the string of the characters encountered thus far. You may not necessarily get the longest character run. The parser reserves the right to hand these to the delegate as potentially many calls in a row to -parser:foundCharacters:
    func parser(parser: NSXMLParser!, foundIgnorableWhitespace whitespaceString: String!){

    }

    // The parser reports ignorable whitespace in the same way as characters it's found.
    func parser(parser: NSXMLParser!, foundProcessingInstructionWithTarget target: String!, data: String!){

    }

    // The parser reports a processing instruction to you using this method. In the case above, target == @"xml-stylesheet" and data == @"type='text/css' href='cvslog.css'"
    func parser(parser: NSXMLParser!, foundComment comment: String!){

    }

    // this gives the delegate an opportunity to resolve an external entity itself and reply with the resulting data.
    func parser(parser: NSXMLParser!, parseErrorOccurred parseError: NSError!){

    }

    // ...and this reports a fatal error to the delegate. The parser will stop parsing.    
    func parser(parser: NSXMLParser!, validationErrorOccurred validationError: NSError!){

    }
}

...
let xmlParser:NSXMLParser = NSXMLParser(contentsOfURL: filePath)!;

xmlParser.delegate = XMLParserDelegate()

if xmlParser.parse() {
    Console.Debug("success")
} else {
    Console.Debug("failed")
}

其中文件路径类似于file:///Users/sss/Documents/app.xml

为什么此代码始终打印失败..实际的URL是NSURL文件,扩展名为&#34; .xml&#34;。这个NSURL来自NSOpenPanel。

0 个答案:

没有答案