Veeru
Veeru

Reputation: 4936

Checking for Valid xml - TBXML crashes if invalid xml

Hi there In my app, everything working fine with handling xml and parsing it. But since the xml is from a remote source, i would like to add some code that will check if the xml is valid before parsing it; mainly because TBXML crashes the app if the xml is not valid.

How can i check if the xml is valid ? I tried to use "try" and "catch" but the error is not being caught; this is how my code is

TBXMLElement * root ;   
@try {
root = tbxml.rootXMLElement;
}
@catch (NSException * e) {      
NSLog(@"error");
}

What am i doing wrong?

Upvotes: 1

Views: 794

Answers (2)

Raxit
Raxit

Reputation: 824

You can check using an if statement whether it is nil or not. See the below example.

TBXMLElement * rootXMLElement = tbxml.rootXMLElement;

    if (rootXMLElement != nil)

        NSLog(@"ok");
        else 
        NSLog(@"Can't proceed: XMLElement is nil");

Upvotes: 0

Michael Ruml
Michael Ruml

Reputation: 58

You can check whether there is a valid root element. Someithing like this:

if ([[TBXML elementName:parser.rootXMLElement] isEqualToString:@"fooBar"])
...

Upvotes: 2

Related Questions