-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDDXMLDocument.m
executable file
·111 lines (92 loc) · 3.09 KB
/
DDXMLDocument.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#import "DDXMLDocument.h"
#import "NSStringAdditions.h"
#import "DDXMLPrivate.h"
@implementation DDXMLDocument
/**
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
*
* If the wrapper object already exists, it is retained/autoreleased and returned.
* Otherwise a new wrapper object is alloc/init/autoreleased and returned.
**/
+ (id)nodeWithPrimitive:(xmlKindPtr)kindPtr
{
// If a wrapper object already exists, the _private variable is pointing to it.
xmlDocPtr doc = (xmlDocPtr)kindPtr;
if(doc->_private == NULL)
return [[[DDXMLDocument alloc] initWithCheckedPrimitive:kindPtr] autorelease];
else
return [[((DDXMLDocument *)(doc->_private)) retain] autorelease];
}
/**
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
*
* The given node is checked, meaning a wrapper object for it does not already exist.
**/
- (id)initWithCheckedPrimitive:(xmlKindPtr)kindPtr
{
self = [super initWithCheckedPrimitive:kindPtr];
return self;
}
/**
* Initializes and returns a DDXMLDocument object created from an NSData object.
*
* Returns an initialized DDXMLDocument object, or nil if initialization fails
* because of parsing errors or other reasons.
**/
- (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error
{
return [self initWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:mask error:error];
}
/**
* Initializes and returns a DDXMLDocument object created from an NSData object.
*
* Returns an initialized DDXMLDocument object, or nil if initialization fails
* because of parsing errors or other reasons.
**/
- (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error
{
if(data == nil || [data length] == 0)
{
if(error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:0 userInfo:nil];
[self release];
return nil;
}
// Even though xmlKeepBlanksDefault(0) is called in DDXMLNode's initialize method,
// it has been documented that this call seems to get reset on the iPhone:
// http://code.google.com/p/kissxml/issues/detail?id=8
//
// Therefore, we call it again here just to be safe.
xmlKeepBlanksDefault(0);
xmlDocPtr doc = xmlParseMemory([data bytes], [data length]);
if(doc == NULL)
{
if(error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:nil];
[self release];
return nil;
}
return [self initWithCheckedPrimitive:(xmlKindPtr)doc];
}
/**
* Returns the root element of the receiver.
**/
- (DDXMLElement *)rootElement
{
xmlDocPtr doc = (xmlDocPtr)genericPtr;
// doc->children is a list containing possibly comments, DTDs, etc...
xmlNodePtr rootNode = xmlDocGetRootElement(doc);
if(rootNode != NULL)
return [DDXMLElement nodeWithPrimitive:(xmlKindPtr)rootNode];
else
return nil;
}
- (NSData *)XMLData
{
return [[self XMLString] dataUsingEncoding:NSUTF8StringEncoding];
}
- (NSData *)XMLDataWithOptions:(NSUInteger)options
{
return [[self XMLStringWithOptions:options] dataUsingEncoding:NSUTF8StringEncoding];
}
@end