0

我有这个xml:

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

        </AccountsList>

为了解析它,我使用

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

但如果 xml 没有 COD:

            <AccountsList>
               <Account 

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

我有崩溃!我如何检查存在

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

或不?

如果结构没有帮助:(

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

它总是返回 TRUE

解决方案:

现在我尝试检查空字符串,这对我有帮助。

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

1 回答 1

1

您可以使用以下代码检索属性值:

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;
}
于 2013-09-19T09:44:54.770 回答