Skip to content
Browse files

added more NSXML compatibility methods

  • Loading branch information...
1 parent 7bd9f17 commit fcfe14b532a7e061c64deb15d12f7955030ce166 @DaGaMs DaGaMs committed Jul 18, 2012
View
2 Source/CXMLElement_CreationExtensions.h
@@ -37,4 +37,6 @@
- (void)addNamespace:(CXMLNode *)inNamespace;
+- (void)removeAttributeForName:(NSString *)name;
+- (id)initWithXMLString:(NSString *)string error:(NSError **)error;
@end
View
61 Source/CXMLElement_CreationExtensions.m
@@ -30,30 +30,69 @@
// or implied, of toxicsoftware.com.
#import "CXMLElement_CreationExtensions.h"
+#import "CXMLNode_CreationExtensions.h"
+#import "CXMLDocument.h"
@implementation CXMLElement (CXMLElement_CreationExtensions)
+- (id)initWithXMLString:(NSString *)string error:(NSError **)error
+{
+ CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:string options:nil error:error];
+ if (!doc)
+ {
+ return nil;
+ }
+
+ if ((self = [doc rootElement]))
+ {
+ [self detach];
+ }
+ return self;
+}
+
- (void)addChild:(CXMLNode *)inNode
{
-NSAssert(inNode->_node->doc == NULL, @"Cannot addChild with a node that already is part of a document. Copy it first!");
-NSAssert(self->_node != NULL, @"_node should not be null");
-NSAssert(inNode->_node != NULL, @"_node should not be null");
-xmlAddChild(self->_node, inNode->_node);
-// now XML element is tracked by document, do not release on dealloc
-inNode->_freeNodeOnRelease = NO;
+ NSAssert(inNode->_node->doc == NULL, @"Cannot addChild with a node that already is part of a document. Copy it first!");
+ NSAssert(self->_node != NULL, @"_node should not be null");
+ NSAssert(inNode->_node != NULL, @"_node should not be null");
+ xmlAddChild(self->_node, inNode->_node);
+ // now XML element is tracked by document, do not release on dealloc
+ inNode->_freeNodeOnRelease = NO;
}
- (void)addNamespace:(CXMLNode *)inNamespace
{
-xmlSetNs(self->_node, (xmlNsPtr)inNamespace->_node);
+ xmlSetNs(self->_node, (xmlNsPtr)inNamespace->_node);
}
- (void)setStringValue:(NSString *)inStringValue
{
-NSAssert(inStringValue != NULL, @"CXMLElement setStringValue should not be null");
-xmlNodePtr theContentNode = xmlNewText((const xmlChar *)[inStringValue UTF8String]);
-NSAssert(self->_node != NULL, @"_node should not be null");
-xmlAddChild(self->_node, theContentNode);
+ NSAssert(inStringValue != NULL, @"CXMLElement setStringValue should not be null");
+ xmlNodePtr theContentNode = xmlNewText((const xmlChar *)[inStringValue UTF8String]);
+ NSAssert(self->_node != NULL, @"_node should not be null");
+ xmlAddChild(self->_node, theContentNode);
+}
+
+- (void)removeAttributeForName:(NSString *)name
+{
+ CXMLNode *node = [self attributeForName:name];
+ xmlUnlinkNode(node->_node);
+ xmlFree(node->_node);
+}
+
+- (void)setChildren:(NSArray *)children
+{
+ for (CXMLNode *child in [[self children] copy])
+ {
+ BOOL alreadyContained = [self.children containsObject:child];
+ xmlUnlinkNode(child->_node);
+ if (!alreadyContained) xmlFree(child->_node);
+ }
+
+ for (CXMLNode *child in children)
+ {
+ [self addChild:child];
+ }
}
@end
View
4 Source/CXMLNode.h
@@ -68,8 +68,8 @@ typedef enum {
- (CXMLNode *)childAtIndex:(NSUInteger)index;
- (CXMLNode *)previousSibling;
- (CXMLNode *)nextSibling;
-//- (CXMLNode *)previousNode;
-//- (CXMLNode *)nextNode;
+- (CXMLNode *)previousNode;
+- (CXMLNode *)nextNode;
//- (NSString *)XPath;
- (NSString *)localName;
- (NSString *)prefix;
View
12 Source/CXMLNode.m
@@ -208,8 +208,16 @@ - (CXMLNode *)nextSibling
return([CXMLNode nodeWithLibXMLNode:_node->next freeOnDealloc:NO]);
}
-//- (CXMLNode *)previousNode;
-//- (CXMLNode *)nextNode;
+- (CXMLNode *)nextNode
+{
+ return [self nextSibling];
+}
+
+- (CXMLNode *)previousNode
+{
+ return [self previousSibling];
+}
+
//- (NSString *)XPath;
- (NSString *)localName
View
2 Source/Creation/CXMLNode_CreationExtensions.h
@@ -52,5 +52,5 @@
//+ (id)DTDNodeWithXMLString:(NSString *)string;
- (void)setStringValue:(NSString *)inStringValue;
-
+- (void)detach;
@end
View
6 Source/Creation/CXMLNode_CreationExtensions.m
@@ -101,5 +101,11 @@ - (void)setStringValue:(NSString *)inStringValue
xmlNodeSetContent(_node, (const xmlChar *)[inStringValue UTF8String]);
}
+- (void)detach
+{
+ xmlUnlinkNode(self->_node);
+ xmlFree(_node);
+}
+
@end

0 comments on commit fcfe14b

Please sign in to comment.
Something went wrong with that request. Please try again.