0

The problem is, that I get a TBXML element back from initWithURL method of TBXML class. I'd like to save this TBXML document as it is, in order to parse it afterwards, when the user is offline as well, but I can't seem to find a method to get the NSString value of the whole object.

As I am new to Objective-C, this might be something really easy as well, as I didn't see any other problems as these. I hope you can help me and that this answer could be helpful for others.

ChillY
  • 106
  • 6

1 Answers1

0

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

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • This does not work in my case due to the fact, that xml can go several levels in depth. the textForElement method will only look for the actual text value of given element. In my case (and probably most cases) XML's go in-depth and it is dynamic - will not always have the same subelements. Basically, this logs out an empty string for me. – ChillY Mar 03 '14 at 12:24
  • @ChillY I have updated my answer with a code sample which lets you pop though each element. you can use this to build your NSString. – Woodstock Mar 03 '14 at 12:37
  • The updated answer, yes I will be able to dynamically go through all the elements like that, but is way too much empty work for such a simple task. I'm rather hoping, that there's a way to get the response string directly from the response instead of the TBXML element as the response of init. – ChillY Mar 03 '14 at 14:55
  • @ChillY have you looked at NSXMLParser? – Woodstock Mar 03 '14 at 14:57
  • I have not yet, due to my whole application using TBXML already. It would be quite time consuming to replace the parser now. Although if there is no way to get element in string form in TBXML, I guess I don't have another choice and will be disappointed in TBXML – ChillY Mar 03 '14 at 15:01