diff --git a/Sources/AndroidXML/Xml_Parser.swift b/Sources/AndroidXML/Xml_Parser.swift index 5b4b6cc..fad8ccf 100644 --- a/Sources/AndroidXML/Xml_Parser.swift +++ b/Sources/AndroidXML/Xml_Parser.swift @@ -90,6 +90,22 @@ public struct Xml_Parser { // No `lookupFn` because attribute values are likely unique try asDict().mapValues { $0.resolve(stringPool)} } + + /// Convenience method If you just need a single attribute. + /// This is slightly more efficient than generating `asDict()` and then throwing away everything else. + /// But if you need two or more values, you should use `asDict()`. + public func get(_ attrName: String) throws -> ResValue? { + for attr in try element.attributes() { + var key = lookupFn(attr.name) + if let prefix = namespaces[attr.ns] { + key = prefix + ":" + key + } + if key == attrName { + return attr.typedValue + } + } + return nil + } } /// Iterate over whole tree and call `start` and `end` blocks for each element.