我有一个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])它总是返回真
解决方案:
现在我试着检查一下空字符串,这对我有帮助。
if ([TBXML valueOfAttributeNamed:@"Cod" forElement:element] isEqualToString:@"")发布于 2013-09-19 09:44:54
可以使用以下代码检索属性值:
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;
}https://stackoverflow.com/questions/18890674
复制相似问题