0

I am creating an iPhone App in which I have to consume values from a service url.The response from url is of XML format.XML file has same name attributes many times,and those attributes are not static.I mean the no of attributes may increase from time to time. So I couldn't understand how to consume that XML response in iPhone.

My XML response looks like this :

GetUserResponse xmlns="http://schemas.datacontract.org/2004/07" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <CompanyList>
      <User>
         <Address>
              <City>Alabaster</City>                   
              <State>Alabama</State>
          </Address>
          <Employee>
              <Name>DrJohn</Name>
          </Employee>
      </User>
      <User>
          <Address>
              <City>SanFransisco</City>>             
              <State>California</State>
          </Address>
          <Employee>
              <Name>DrWilliams</Name>
          </Employee>
      </User>
    </CompanyList>
    </GetUserResponse>

The thing is I couldn't say there will be specific number of tags as of 2 tags here.They may or may not increase from time to time.I think we should take something like count number of items and extract the values but couldn't understand how?

Talha
  • 12,673
  • 5
  • 49
  • 68
cutiepie
  • 29
  • 1
  • 8
  • May below link helps you [http://stackoverflow.com/questions/12471774/cant-retrieve-xml-values-using-nsxmlparser/12472090#12472090][1] [1]: http://stackoverflow.com/questions/12471774/cant-retrieve-xml-values-using-nsxmlparser/12472090#12472090 – Romit M. Sep 26 '12 at 06:12
  • IN above xml every tag is different then what is the issue ? – V-Xtreme Sep 26 '12 at 06:29
  • I do have two tags and have to consume values from them .And the no of tags may increase ....Should I take them into array or....?? – cutiepie Sep 26 '12 at 06:54

1 Answers1

0

This is one of many ways to do it. You can simply get the attributeDict and parse all the values from it using key-value.

NSString *element;

- (void) viewDidLoad {

    [super viewDidLoad];
    cityArr = [[NSMutableArray alloc] init];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([elementName isEqualToString:@“Address”]) {

        // initialize stings to store values of child elements
        cityStr = [NSMutableString alloc] init];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if ([element isEqualToString:@“City”])

         cityStr appendString:string];// Append Values
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@“Address”]) {

        [cityArr addObject:cityStr]; // Add strings to array/dictionary and release string
    }
}
Nina
  • 1,579
  • 2
  • 21
  • 51
  • Can u please tell me how to initialise strings ,how to append values to arrays..?Because I am a new bie to mobile platforms I couldnot understand anything .... – cutiepie Sep 26 '12 at 10:51
  • hey what If,If I want to consume Line1 tags?THis is the code Iam writing..didStartElement{ if ([elementName isEqualToString:@"Address"]) {cityStr = [[NSMutableString alloc] init];line1Str= [[NSMutableString alloc] init]; }foundCharacters{ if ([element isEqualToString:@"City"]){ [cityStr appendString:string];} else if([element isEqualToString:@"Line1"]){[line1Str appendString:string];} didEndElement{ if ([elementName isEqualToString:@"Address"]) {[cityArr addObject:cityStr];[line1Arr addObject:line1Str];} NSLog(@"%@",cityArr);NSLog(@"%@",line1Str);.Here line1Arr is printing null values. – cutiepie Sep 28 '12 at 06:21
  • I didn’t see any mistake in your code.. Where you are using `NSLog`?? What about cityArr value?! If you are printing the string value out of `NSXMLParser` delegate methods, it will be null.... – Nina Oct 01 '12 at 04:21