swift 代码:
<span style="font-size:24px;"> @IBAction func ReadXml(sender: AnyObject) { let parser = NSXMLParser(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("data",ofType: "xml")!)) parser!.delegate = self parser?.parse() } var currentNodeName: String! func parser(parser: NSXMLParser,didStartElement elementName: String,namespaceURI: String?,qualifiedName qName: String?,attributes attributeDict: [String : String]) { currentNodeName = elementName /* 提取属性 */ if elementName == "person" { let age: String! = attributeDict["age"] print("currentNodeName:\(currentNodeName) age:\(age)") } } func parser(parser: NSXMLParser,foundCharacters string: String) { let str = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) if str != "" {// 提取数据 print("current node:\(currentNodeName),value: \(str)") } } </span>@H_301_7@ 结果: