gpt4 book ai didi

iphone - TBXML 解析名为属性的值

转载 作者:行者123 更新时间:2023-11-29 03:37:52 25 4
gpt4 key购买 nike

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

最佳答案

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

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;
}

关于iphone - TBXML 解析名为属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18890674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com