feat: get single attribute by key
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user