1

i want to parse the XML file. i am enable to parse simple XML file. but there are little complex XML file

<?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>

Here i want to get the value of <p> tag (i mean i want to get the value of attribute id which is 327) Please suggest

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rupesh
  • 7,866
  • 11
  • 41
  • 58

2 Answers2

2

You can use below code to find your "id" value in XML parsing method,

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

}
Andy Dent
  • 17,578
  • 6
  • 88
  • 115
Jay Vachhani
  • 696
  • 5
  • 16
  • in my case i have declared int *count in .h file and used it as count = [[attributeDict objectForKey:@"count"] intValue]; in .m file but it gives a warning " warning: assignment makes pointer from integer without a cast " – Maulik Apr 11 '11 at 09:38
  • Y you wrote int * count? It should be int count; only. – Jay Vachhani Apr 11 '11 at 22:45
0

In your (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDictmethod, the attributeDict argument contains all the attributes. For example, to access the 'id' attribute, simply call [attributeDict objectForKey:@"id"].