feat: get single attribute by key

This commit is contained in:
relikd
2025-11-29 02:21:46 +01:00
parent fd522d612f
commit d9fe646bcc

View File

@@ -90,6 +90,22 @@ public struct Xml_Parser {
// No `lookupFn` because attribute values are likely unique // No `lookupFn` because attribute values are likely unique
try asDict().mapValues { $0.resolve(stringPool)} 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. /// Iterate over whole tree and call `start` and `end` blocks for each element.