【问题标题】:TBXML parsing value of attribute named命名属性的 TBXML 解析值
【发布时间】:2013-09-19 09:15:21
【问题描述】:

我有这个 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:@"")

【问题讨论】:

    标签: iphone ios xml parsing tbxml


    【解决方案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;
    }
    

    【讨论】:

    • 属性 将始终有效,因为 AccountNumber、AccountName、AccountSecondName 仍然存在!
    • @Rubik:是的,但是你可以设置一个 if 条件来检查 Cod 属性是否存在。如果存在,请执行相应的操作。
    猜你喜欢
    • 2011-07-16
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2013-07-08
    • 2021-04-14
    • 1970-01-01
    • 2012-07-05
    相关资源
    最近更新 更多