Index: src/OFXMLElement.h
==================================================================
--- src/OFXMLElement.h
+++ src/OFXMLElement.h
@@ -309,6 +309,20 @@
* Adds a child to the OFXMLElement.
*
* \param child Another OFXMLElement which is added as a child
*/
- (void)addChild: (OFXMLElement*)child;
+
+/**
+ * \param elemname The name of the elements
+ * \return The child elements with the specified name
+ */
+- (OFArray*)elementsForName: (OFString*)elemname;
+
+/**
+ * \param elemname The name of the elements
+ * \param elemns The namespace of the elements
+ * \return The child elements with the specified name and namespace
+ */
+- (OFArray*)elementsForName: (OFString*)elemname
+ namespace: (OFString*)elemns;
@end
Index: src/OFXMLElement.m
==================================================================
--- src/OFXMLElement.m
+++ src/OFXMLElement.m
@@ -573,10 +573,38 @@
if (children == nil)
children = [[OFMutableArray alloc] init];
[children addObject: child];
}
+
+- (OFArray*)elementsForName: (OFString*)elemname
+{
+ return [self elementsForName: elemname
+ namespace: nil];
+}
+
+- (OFArray*)elementsForName: (OFString*)elemname
+ namespace: (OFString*)elemns
+{
+ OFMutableArray *ret = [OFMutableArray array];
+ OFXMLElement **children_c = [children cArray];
+ size_t i, children_count = [children count];
+
+ if (elemns != nil) {
+ for (i = 0; i < children_count; i++)
+ if ([children_c[i]->ns isEqual: elemns] &&
+ [children_c[i]->name isEqual: elemname])
+ [ret addObject: children_c[i]];
+ } else {
+ for (i = 0; i < children_count; i++)
+ if (children_c[i]->ns == nil &&
+ [children_c[i]->name isEqual: elemname])
+ [ret addObject: children_c[i]];
+ }
+
+ return ret;
+}
- (void)dealloc
{
[name release];
[ns release];
Index: tests/OFXMLElementTests.m
==================================================================
--- tests/OFXMLElementTests.m
+++ tests/OFXMLElementTests.m
@@ -29,10 +29,11 @@
@implementation TestsAppDelegate (OFXMLElementTests)
- (void)XMLElementTests
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFXMLElement *elem[4];
+ OFArray *a;
TEST(@"+[elementWithName:]",
(elem[0] = [OFXMLElement elementWithName: @"foo"]) &&
[[elem[0] stringValue] isEqual: @""])
@@ -114,9 +115,15 @@
@""] &&
R([elem[2] addChild: [OFXMLElement elementWithName: @"bar"
namespace: @"urn:objfw:test"]]) &&
[[elem[2] stringValue] isEqual:
@""])
+
+ TEST(@"-[elementsForName:namespace:]",
+ (a = [elem[2] elementsForName: @"bar"
+ namespace: @"urn:objfw:test"]) &&
+ [a count] == 1 && [[[a firstObject] stringValue] isEqual:
+ @""])
[pool drain];
}
@end