I personally wouldn't use TBXML, it's old and clunky plus Apple has it's own NSXMLParser
class, however you can do it like this, assuming you have an TBXML instance called 'tbxml':
TBXMLElement *root = tbxml.rootXMLElement;
NSString *stringFromXML = [TBXML textForElement:root];
NSLog(@"XML as String: %@",stringFromXML);
All that I'm doing here is getting the 'root' element basically the whole document in your case.
Using a class method on TBXML to extract the 'text' for the root element, and store this in an NSString.
You can then use whatever method you'd like to store this NSString or it's value.
To traverse an unknown or dynamic XML input:
- (void)loadUnknownXML {
// Load and parse the test.xml file
tbxml = [[TBXML tbxmlWithXMLFile:@"test.xml"] retain];
// If TBXML found a root node, process element and iterate all children
if (tbxml.rootXMLElement)
[self traverseElement:tbxml.rootXMLElement];
- (void) traverseElement:(TBXMLElement *)element {
do {
// Display the name of the element
NSLog(@"%@",[TBXML elementName:element]);
// Obtain first attribute from element
TBXMLAttribute * attribute = element->firstAttribute;
// if attribute is valid
while (attribute) {
// Display name and value of attribute to the log window
NSLog(@"%@->%@ = %@",
[TBXML elementName:element],
[TBXML attributeName:attribute],
[TBXML attributeValue:attribute]);
// Obtain the next attribute
attribute = attribute->next;
}
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
}
Regards,
John