1 // A slightly modified version of Robert Harder's XMLTree, which he released into the public domain.
4 #import <Cocoa/Cocoa.h>
8 XMLTree provides an Objective-C wrapper for Apple's built-in C-language
9 XML parser and manipulation functions.
15 @abstract Wraps some C-level functions from Apple for XML manipulation.
18 XMLTree provides an Objective-C wrapper for Apple's built-in C-language
19 XML parser and manipulation functions.
20 At the moment it only supports basic element and attribute information.
21 However Apple's XML parser supports processing instructions, CDATA,
22 and some other things I've never seen, so I'll add support for these
26 I'm releasing this code into the Public Domain, so you can include it
27 with your software regardless of the license you use. If you make any
28 useful additions or bug fixes (especially with retain/release), we
29 would all appreciate it if you would let me know so we can give the
30 changes to everyone else too.
32 <p>Author: Robert Harder, rob -at- iharder.net</p>
35 @interface XMLTree
: NSObject
40 NSString
*_lastUnknownSelector
;
41 NSString
*_namespacePrefix
;
47 @abstract Be a good citizen and clean up after ourselves.
54 @abstract Creates an autoreleased XMLTree with the contents of <var>url</var>.
56 Creates an autoreleased XMLTree with the contents of <var>url</var> or
57 <tt>nil</tt> if there was an error.
58 Of course the URL can be pointing to a file or a URL on the internet
59 such as a GET command to a SOAP application.
60 @param url The <tt>NSURL</tt> pointing to your XML data.
61 @result An autoreleased <tt>XMLTree</tt> with the contents
62 of <var>url</var> or <tt>nil</tt> if there was a problem.
64 +(XMLTree
*)treeWithURL
:(NSURL
*)url
;
71 @abstract Initializes and returns an <tt>XMLTree</tt>.
73 Initializes and returns an <tt>XMLTree</tt> (with a retain count of 1).
74 There isn't much point to creating an <tt>XMLTree</tt> this way until
75 I add methods for manuallying adding XML nodes to the tree.
76 @result An <tt>XMLTree</tt> (with a retain count of 1).
85 @abstract Initializes and returns an <tt>XMLTree</tt>
86 with the contents of <var>url</var>.
88 Initializes and returns an <tt>XMLTree</tt> (with a retain count of 1)
89 with the XML contents of <var>url</var> or <tt>nil</tt> if there is an error.
90 @param url The <tt>NSURL</tt> pointing to your XML data.
91 @result An <tt>XMLTree</tt> with a retain count of 1.
93 -(XMLTree
*)initWithURL
:(NSURL
*)url
;
101 @method initWithCFXMLTreeRef:
102 @abstract Initializes and returns an <tt>XMLTree</tt>
103 with the internal data represented by <var>ref</var>.
105 Initializes and returns an <tt>XMLTree</tt> (with a retain count of 1)
106 with the internal <tt>CFXMLTreeRef</tt> data represented by <var>ref</var>.
107 You probably won't ever need to call this yourself, but I call it internally
108 and may move it to a Private API in the XMLTree.m file later.
109 @param ref The <tt>CFXMLTreeRef</tt> containing the XML data.
110 @result An <tt>XMLTree</tt> with a retain count of 1.
112 -(XMLTree
*)initWithCFXMLTreeRef
:(CFXMLTreeRef
)ref
;
117 @method initWithData:withResolvingURL:
118 @abstract Initializes and returns an <tt>XMLTree</tt>
119 with the XML data stored in <var>inData</var> and
120 an optional URL used to resolve references.
122 Initializes and returns an <tt>XMLTree</tt> (with a retain count of 1)
123 with the internal <tt>NSData</tt> data represented by <var>inData</var>.
124 The <tt>NSURL</tt> url is optional. If provided, Apple's XML parser
125 may use it to resolve links in the XML code.
126 @param inData The <tt>NSData</tt> containing the XML data.
127 @param url The <tt>NSURL</tt> for resolving links.
128 @result An <tt>XMLTree</tt> with a retain count of 1.
130 - (XMLTree
*)initWithData
:(NSData
*)inData withResolvingURL
:(NSURL
*)url
;
134 @method initWithData:
135 @abstract Initializes and returns an <tt>XMLTree</tt>
136 with the XML data stored in <var>inData</var>.
138 Initializes and returns an <tt>XMLTree</tt> (with a retain count of 1)
139 with the internal <tt>NSData</tt> data represented by <var>inData</var>.
140 @param inData The <tt>NSData</tt> containing the XML data.
141 @result An <tt>XMLTree</tt> with a retain count of 1.
143 - (XMLTree
*)initWithData
:(NSData
*)inData
;
146 /* ******** A B O U T S E L F ******** */
151 @abstract Returns the name of the root node in the tree.
153 Returns the name of the root node in the tree or <tt>nil</tt>
154 if a name is not appropriate in the current context such as
155 if the "tree" is actually a single XML Processing Instruction node.
156 @result The name of the root node in the tree..
163 @abstract Returns the <tt>XMLTree</tt> in an XML-looking form.
165 Returns the <tt>XMLTree</tt> in an XML-looking form as performed
166 by Apple's own <tt>CFXMLTreeCreateXMLData(...)</tt> method.
167 @result The <tt>XMLTree</tt> in an XML-looking form.
175 @abstract Returns a textual representation of the <tt>XMLTree</tt>.
178 Returns a textual representation of the <tt>XMLTree</tt>.
179 The way the tree is interpreted depends on what kind of root
180 node is represented by the receiver.
183 Listed below are the actions this method takes depending
184 on the type of node this is.
189 <th>Node Type</th><th>CFXMLNodeTypeCode</th><th>Action</th>
194 <td>Document</td><td>kCFXMLNodeTypeDocument</td>
195 <td rowspan="2" valign="top">
196 Recursively descends XML document piecing together
197 the Text and CDATA nodes that are encountered.
198 You can think of this as returning the plaintext
199 version of the XML data, that is, with all tags removed.
202 <td>Element</td><td>kCFXMLNodeTypeElement</td>
205 <td>Attribute</td><td>kCFXMLNodeTypeAttribute</td>
206 <td rowspan="13" valign="top">
207 Default action: Whatever is returned by Apple's
208 <tt>CFXMLNodeGetString(...)</tt> method.
211 <tr><td>Processing Instruction</td><td>kCFXMLNodeTypeProcessingInstruction</td></tr>
212 <tr><td>Comment</td><td>kCFXMLNodeTypeComment</td></tr>
213 <tr><td>Text</td><td>kCFXMLNodeTypeText</td></tr>
214 <tr><td>CDATA Section</td><td>kCFXMLNodeTypeCDATASection</td></tr>
215 <tr><td>Document Fragment</td><td>kCFXMLNodeTypeDocumentFragment</td></tr>
216 <tr><td>Entity</td><td>kCFXMLNodeTypeEntity</td></tr>
217 <tr><td>Entity Reference</td><td>kCFXMLNodeTypeEntityReference</td></tr>
218 <tr><td>Document Type</td><td>kCFXMLNodeTypeDocumentType</td></tr>
219 <tr><td>Whitespace</td><td>kCFXMLNodeTypeWhitespace</td></tr>
220 <tr><td>Notation Element</td><td>kCFXMLNodeTypeNotation</td></tr>
221 <tr><td>Element Type Declaration</td><td>kCFXMLNodeTypeElementTypeDeclaration</td></tr>
222 <tr><td>Attribute List Declaration</td><td>kCFXMLNodeTypeAttributeListDeclaration</td></tr>
227 @result A textual representation of the <tt>XMLTree</tt>.
229 -(NSString
*)description
;
233 @method attributeNamed:
234 @abstract Returns the attribute named <var>name</var>.
236 Returns the attribute named <var>name</var> or
237 <tt>nil</tt> if no such attribute is found or
238 the node is not an Element node.
239 @param name The name of the attribute to return.
240 @result The attribute named <var>name</var>.
242 -(NSString
*)attributeNamed
:(NSString
*)name
;
248 @abstract Returns a dictionary of all the attributes.
250 Returns a dictionary of all the attributes in the node
251 or <tt>nil</tt> if the node is not an Element node.
252 @result A dictionary of all the attributes.
254 -(NSDictionary
*)attributes
;
259 @abstract Returns the type of node this is.
261 Returns the type of node this is as defined by Apple's
264 enum CFXMLNodeTypeCode {
265 kCFXMLNodeTypeDocument = 1,
266 kCFXMLNodeTypeElement = 2,
267 kCFXMLNodeTypeAttribute = 3,
268 kCFXMLNodeTypeProcessingInstruction = 4,
269 kCFXMLNodeTypeComment = 5,
270 kCFXMLNodeTypeText = 6,
271 kCFXMLNodeTypeCDATASection = 7,
272 kCFXMLNodeTypeDocumentFragment = 8,
273 kCFXMLNodeTypeEntity = 9,
274 kCFXMLNodeTypeEntityReference = 10,
275 kCFXMLNodeTypeDocumentType = 11,
276 kCFXMLNodeTypeWhitespace = 12,
277 kCFXMLNodeTypeNotation = 13,
278 kCFXMLNodeTypeElementTypeDeclaration = 14,
279 kCFXMLNodeTypeAttributeListDeclaration = 15
282 @result The type of node this is.
284 -(CFXMLNodeTypeCode
)type
;
289 /* ******** A B O U T P A R E N T ******** */
295 @abstract Returns the parent of the tree.
297 Returns the parent of the tree or <tt>nil</tt>
298 if the parent does not exist.
299 @result The tree's parent.
305 /* ******** A B O U T C H I L D R E N ******** */
312 @abstract Returns the value indicated by the xpath.
314 Returns the value indicated by the xpath.
315 Only basic xpath values are supported at this time.
316 Basically it supports accessing elements and attributes.
317 @param xpath The xpath to evaluate
318 @result The XMLTree or NSString of the result.
320 -(id
)xpath
:(NSString
*)xpath
;
325 @method childAtIndex:
326 @abstract Returns the child at the given index.
328 Returns the child at the given index or <tt>nil</tt>
329 if no such child exists or it doesn't make sense
330 to have children (such as a Processing Instruction node).
331 @param index The index of the child to get.
332 @result The child at <var>index</var>.
334 -(XMLTree
*)childAtIndex
:(int)index
;
336 -(NSArray
*)childrenNamed
:(NSString
*)name
;
338 - (XMLTree
*)childWithLocalName
:(NSString
*)localName prefix
:(NSString
*)prefix
;
339 - (NSArray
*)childrenWithLocalName
:(NSString
*)localName prefix
:(NSString
*)prefix
;
344 @abstract Returns the first child named <var>name</var>.
346 Returns the first child named <var>name</var> or <tt>nil</tt>
347 if no such child exists or it doesn't make sense
348 to have children (such as a Processing Instruction node).
349 @param name The name of the child.
350 @result The child named <var>name</var>.
352 -(XMLTree
*)childNamed
:(NSString
*)name
;
356 @method childNamed:withAttribute:equalTo:
357 @abstract Returns the first child named <var>name</var>
358 that has a matching attribute.
360 Returns the first child named <var>name</var> with
361 attribute <var>attrName</var> equal to <var>attrVal</var>
362 or <tt>nil</tt> if no such child exists or it doesn't make sense
363 to have children (such as a Processing Instruction node).
364 @param name The name of the child.
365 @param attrName The name of the attribute to look up.
366 @param attrVal The attribute value to match.
367 @result The matching child.
369 -(XMLTree
*)childNamed
:(NSString
*)name
370 withAttribute
:(NSString
*)attrName
371 equalTo
:(NSString
*)attrVal
;
375 @method descendentNamed:
376 @abstract Returns the first descendent named <var>name</var>.
378 Returns the descendent named <var>name</var> or <tt>nil</tt>
379 if no such descendent exists or it doesn't make sense
380 to have descendents (such as a Processing Instruction node).
381 This is a depth-first search.
382 @param name The name of the child.
383 @result The child named <var>name</var>.
385 -(XMLTree
*)descendentNamed
:(NSString
*)name
;
390 @abstract Returns the number of children in the tree.
392 Returns the number of children in the tree or <tt>-1</tt>
393 if there is no valid tree contained within (like if you
394 tried to create an <tt>XMLTree</tt> with <tt>init</tt>).
395 @result The number of children in the tree.
403 @function XMLTreeDescription
404 @abstract Used internally to recursively generate tree descriptions.
406 Used internally to recursively generate tree descriptions.
407 The returned string is just the mutable string that was passed in.
408 It's retain count is unchanged.
409 @param descr The mutable string that will have descriptions appended to it.
410 @param tree The tree from which to make a description.
411 @result A description of <var>tree</var>.
412 It is the <var>descr</var> that was passed in.
414 CFStringRef
XMLTreeDescription( CFMutableStringRef descr
, CFXMLTreeRef tree
);
419 @function XMLTreeDescendentNamed
420 @abstract Used internally to recursively search for a descendent.
422 Used internally to recursively search for a descendent.
423 The tree that is returned, if any, has a retain count of one more
424 than it ought (it cannot be autoreleased, so far as I know), so
425 you are responsible for releasing it, whether or not you retain it.
426 @param name The name of the descendent to search for.
427 @param tree The tree in which to recursively search.
428 @result The matching descendent or <tt>NULL</tt> if no descendent is found.
430 CFXMLTreeRef
XMLTreeDescendentNamed( CFStringRef name
, CFXMLTreeRef tree
);
435 CFTypeRef
XMLTreeXPath( CFMutableStringRef xpath
, CFXMLTreeRef tree
);