0

I have this xml:

            <AccountsList>
               <Account 
            Cod="0000" 
            AccountNumber="12345" 
            AccountName="John" 
            AccountSecondName="Wilson" />

        </AccountsList>

For parsing it I use

            [TBXML valueOfAttributeNamed:@"Cod" forElement:element]
            [TBXML valueOfAttributeNamed:@"AccountNumber" forElement:element]
            [TBXML valueOfAttributeNamed:@"AccountName" forElement:element]
            [TBXML valueOfAttributeNamed:@"AccountSecondName" forElement:element]

But if xml came without COD:

            <AccountsList>
               <Account 

            AccountNumber="12345" 
            AccountName="John" 
            AccountSecondName="Wilson" 
                        />
        </AccountsList>

I have crash! How I can check exist

            [TBXML valueOfAttributeNamed:@"Cod" forElement:element]

or not?

if struct don't help :(

             if ([TBXML valueOfAttributeNamed:@"Cod" forElement:element])

It's always return TRUE

Solution:

Now I try to check for empty string and this help me.

          if ([TBXML valueOfAttributeNamed:@"Cod" forElement:element] isEqualToString:@"")
rubik
  • 572
  • 4
  • 15

1 Answers1

1

You can use the following code for retrieving the attribute values:

TBXMLAttribute * attribute = element->firstAttribute;

//Checking attribute is valid or not
while (attribute)
{
    //Here you can check the `Cod` attribute exist or not
    NSLog(@"%@->%@ = %@",[TBXML elementName:element],[TBXML attributeName:attribute], TBXML attributeValue:attribute]);

    // Next attribute
    attribute = attribute->next;
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Attribute will be always valid because AccountNumber, AccountName, AccountSecondName remain there! – rubik Sep 19 '13 at 11:24
  • @Rubik: yeah sure, but you can put an if condition for checking if the Cod attribute exist or not. If exist do the corresponding actions. – Midhun MP Sep 19 '13 at 12:07