views:

1070

answers:

2

hi all,

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

+2  A: 

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

}
Jay Vachhani
A: 

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"].

Benjamin Egelund-Müller