Files
Files
Files included with TBXML are:
TBXML.h - Header file containing defines / structures and class definition
TBXML.m - Implementation containing TBXML declaration and private interface
NSDataAdditions.h - Header file containing definition of NSData categories
NSDataAdditions.m - Implementation file containing NSData categories for decoding gzip & base64
TBXMLElement
The TBXMLElement structure holds information about a single XML element. The structure holds the element name & text along with pointers to the first attribute, parent element, first child element and first sibling element. Using this structure, we can create a linked list of TBXMLElements to map out an entire XML file.
Structures
typedef struct _TBXMLElement {
char * name;
char * text;
TBXMLAttribute * firstAttribute;
struct _TBXMLElement * parentElement;
struct _TBXMLElement * firstChild;
struct _TBXMLElement * currentChild;
struct _TBXMLElement * nextSibling;
struct _TBXMLElement * previousSibling;
} TBXMLElement;
TBXMLAttribute
The TBXMLAttribute structure holds information about a single XML attribute. The structure holds the attribute name, value and next sibling attribute. This structure allows us to create a linked list of attributes belonging to a specific element.
typedef struct _TBXMLAttribute {
char * name;
char * value;
struct _TBXMLAttribute * next;
} TBXMLAttribute;
TBXMLElementBuffer
The TBXMLElementBuffer is a structure that holds a buffer of TBXMLElements. When the buffer of elements is used, an additional buffer is created and linked to the previous one. This allows for efficient memory allocation/deallocation elements.
typedef struct _TBXMLElementBuffer {
TBXMLElement * elements;
struct _TBXMLElementBuffer * next;
struct _TBXMLElementBuffer * previous;
} TBXMLElementBuffer;
TBXMLAttributeBuffer
The TBXMLAttributeBuffer is a structure that holds a buffer of TBXMLAttributes. When the buffer of attributes is used, an additional buffer is created and linked to the previous one. This allows for efficient memeory allocation/deallocation of attributes
typedef struct _TBXMLAttributeBuffer {
TBXMLAttribute * attributes;
struct _TBXMLAttributeBuffer * next;
struct _TBXMLAttributeBuffer * previous;
} TBXMLAttributeBuffer;
+ (id)tbxmlWithXMLFile:(NSString*)aXMLFile;
Instantiates a TBXML object and parses the specified file. The following code instantiates a TBXML object and parses books.xml.
Structures
TBXML * tbxml = [[TBXML alloc] initWithXMLFile:@"books.xml"];
- (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtension
Instantiates a TBXML object and parses the specified file. The following code instantiates a TBXML object and parses books.xml.
TBXML * tbxml = [[TBXML alloc] initWithXMLFile:@"books" fileExtension:@"xml"];
- (id)initWithXMLString:(NSString*)aXMLString
Instantiates a TBXML object and parses the specified XML string. The following code instantiates a TBXML object and parses the given XML.
tbxml = [[TBXML alloc] initWithXMLString:@"<root><elem1 attribute1=\"elem1 attribute1\"/><elem2 attribute2=\"elem2 attribute2\"/></root>;"];
- (id)initWithXMLData:(NSData*)aData
Instantiates a TBXML object and parses the specified XML in an NSData object. The following code instantiates a TBXML object and parses an XML document held in an NSData object.
TBXML * tbxml = [[TBXML alloc] initWithXMLData:myXMLData];
- (id)initWithURL:(NSURL*)aURL
Instantiates a TBXML object then downloads and parses an XML file for the given URL. The following code instantiates a TBXML object and parses the note.xml file from w3schools.com.
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.w3schools.com/XML/note.xml"]];
+ (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement
Retrieves the first child element of the specified name from the given parent element. The following code retrieves the first author element from the root element.
TBXMLElement * author = [TBXML childElementNamed:@"author" parentElement:rootXMLElement];
+ (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElement*)aXMLElement
Retrieves the next sibling with the specified name starting from the given element. The following returns the next "author" element starting from the given author element.
TBXMLElement * author = [TBXML nextSiblingNamed:@"author" searchFromElement:author];
+ (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)aXMLElement
Returns an NSString containing the value of the specified attribute belonging to the given element. The following returns the name attribute from the given author element.
NSString * authorName = [TBXML valueOfAttributeNamed:@"name" forElement:authorElement];
+ (NSString*) textForElement:(TBXMLElement*)aXMLElement
Returns an NSString containing the text for the specified element. The following returns the text from the book element.
NSString * bookDescription = [TBXML textForElement:bookElement];
+ (NSString*) elementName:(TBXMLElement*)aXMLElement;
Returns an NSString containing the element name for the specified element.
NSString * elementName = [TBXML elementName:element];
+ (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute;
Returns an NSString containing the attribute name for the specified attribute.
NSString * attributeName = [TBXML attributeName:attribute];
+ (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute;
Returns an NSString containing the attribute value for the specified attribute.
NSString * attributeValue = [TBXML attributeValue:attribute];