| 1 |
#import "DDXMLElementAdditions.h" |
| 2 |
|
| 3 |
@implementation DDXMLElement (DDAdditions) |
| 4 |
|
| 5 |
/** |
| 6 |
* Quick method to create an element |
| 7 |
**/ |
| 8 |
+ (DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns |
| 9 |
{ |
| 10 |
DDXMLElement *element = [DDXMLElement elementWithName:name]; |
| 11 |
[element setXmlns:ns]; |
| 12 |
return element; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* This method returns the first child element for the given name. |
| 17 |
* If no child element exists for the given name, returns nil. |
| 18 |
**/ |
| 19 |
- (DDXMLElement *)elementForName:(NSString *)name |
| 20 |
{ |
| 21 |
NSArray *elements = [self elementsForName:name]; |
| 22 |
if([elements count] > 0) |
| 23 |
{ |
| 24 |
return [elements objectAtIndex:0]; |
| 25 |
} |
| 26 |
else |
| 27 |
{ |
| 28 |
// Note: If you port this code to work with Apple's NSXML, beware of the following: |
| 29 |
// |
| 30 |
// There is a bug in the NSXMLElement elementsForName: method. |
| 31 |
// Consider the following XML fragment: |
| 32 |
// |
| 33 |
// <query xmlns="jabber:iq:private"> |
| 34 |
// <x xmlns="some:other:namespace"></x> |
| 35 |
// </query> |
| 36 |
// |
| 37 |
// Calling [query elementsForName:@"x"] results in an empty array! |
| 38 |
// |
| 39 |
// However, it will work properly if you use the following: |
| 40 |
// [query elementsForLocalName:@"x" URI:@"some:other:namespace"] |
| 41 |
// |
| 42 |
// The trouble with this is that we may not always know the xmlns in advance, |
| 43 |
// so in this particular case there is no way to access the element without looping through the children. |
| 44 |
// |
| 45 |
// This bug was submitted to apple on June 1st, 2007 and was classified as "serious". |
| 46 |
// |
| 47 |
// --!!-- This bug does NOT exist in DDXML --!!-- |
| 48 |
|
| 49 |
return nil; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* This method returns the first child element for the given name and given xmlns. |
| 55 |
* If no child elements exist for the given name and given xmlns, returns nil. |
| 56 |
**/ |
| 57 |
- (DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns |
| 58 |
{ |
| 59 |
NSArray *elements = [self elementsForLocalName:name URI:xmlns]; |
| 60 |
if([elements count] > 0) |
| 61 |
{ |
| 62 |
return [elements objectAtIndex:0]; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
return nil; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the common xmlns "attribute", which is only accessible via the namespace methods. |
| 72 |
* The xmlns value is often used in jabber elements. |
| 73 |
**/ |
| 74 |
- (NSString *)xmlns |
| 75 |
{ |
| 76 |
return [[self namespaceForPrefix:@""] stringValue]; |
| 77 |
} |
| 78 |
|
| 79 |
- (void)setXmlns:(NSString *)ns |
| 80 |
{ |
| 81 |
// If you use setURI: then the xmlns won't be displayed in the XMLString. |
| 82 |
// Adding the namespace this way works properly. |
| 83 |
// |
| 84 |
// This applies to both Apple's NSXML and DDXML. |
| 85 |
|
| 86 |
[self addNamespace:[DDXMLNode namespaceWithName:@"" stringValue:ns]]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Shortcut to avoid having to manually create a DDXMLNode everytime. |
| 91 |
**/ |
| 92 |
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string |
| 93 |
{ |
| 94 |
[self addAttribute:[DDXMLNode attributeWithName:name stringValue:string]]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns all the attributes as a dictionary. |
| 99 |
**/ |
| 100 |
- (NSDictionary *)attributesAsDictionary |
| 101 |
{ |
| 102 |
NSArray *attributes = [self attributes]; |
| 103 |
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[attributes count]]; |
| 104 |
|
| 105 |
uint i; |
| 106 |
for(i = 0; i < [attributes count]; i++) |
| 107 |
{ |
| 108 |
DDXMLNode *node = [attributes objectAtIndex:i]; |
| 109 |
|
| 110 |
[result setObject:[node stringValue] forKey:[node name]]; |
| 111 |
} |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
@end |