如何使用 NSXMLParser 从 SWIFT 中的 XML 获取特定属性

How can I get a specific attribute from a XML in SWIFT with NSXMLParser

我需要专门从 XML File 中检索特定属性 <reading type="NPSI_PM25_3HR" value="xx"/><id>NRS</id> 下,然后将其解析为一个变量,稍后我想打印它,但我在 Swift 中找不到任何方法来执行此操作。有什么办法可以做到这一点?谢谢!

我自己想出来的。这是我为了检索值所做的,以防有人遇到与我相同的问题。

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    if elementName == "reading"{
        if attributeDict["type"] == "NPSI_PM25_3HR"{
            let PSIValue = attributeDict["value"]! as String
            print(i)
            switch i {
                case 0:
                    area = "NRS"
                    nationalPSI = PSIValue
                case 1:
                    area = "North"
                case 2:
                    area = "South"
                case 3:
                    area = "Central"
                case 4:
                    area = "West"
                case 5:
                    area = "East"
                default:
                    area = ""
            }
            i += 1
            print(area, ":", PSIValue)
        }

    }
}