【问题标题】:iPhone : Parse XML file with attributeiPhone:使用属性解析 XML 文件
【发布时间】:2009-10-05 11:05:24
【问题描述】:

我想解析 XML 文件。我能够解析简单的 XML 文件。但是很少有复杂的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<Level>
<p id='327'>
   <Item>
      <Id>5877</Id>
      <Type>0</Type>
      <Icon>---</Icon>
      <Title>Btn1Item1</Title>
   </Item>
   <Item>
      <Id>5925</Id>
      <Type>0</Type>
      <Icon>---</Icon>
      <Title>Btn1Item4</Title>
   </Item>
</p>
</Level>

这里我想获取&lt;p&gt;标签的值(我的意思是我想获取属性id的值,即327) 请推荐

【问题讨论】:

    标签: iphone xml parsing


    【解决方案1】:

    您可以使用下面的代码在 XML 解析方法中找到您的“id”值,

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
        attributes:(NSDictionary *)attributeDict {
    
        if([elementName isEqualToString:@"p"])
        {
    
            int idValue = [[attributeDict objectForKey:@"id"] intValue];
    
            NSLog(@"Reading id value :%d", idValue);
        }
    
    }
    

    【讨论】:

    • 在我的情况下,我在 .h 文件中声明了 int *count 并将其用作 count = [[attributeDict objectForKey:@"count"] intValue];在 .m 文件中,但它给出了警告“警告:赋值使指针从整数不进行强制转换”
    • 你写的是 int * count?它应该是int count;仅限。
    【解决方案2】:

    在您的(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 方法中,attributeDict 参数包含所有属性。例如,要访问“id”属性,只需调用[attributeDict objectForKey:@"id"]

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      相关资源
      最近更新 更多