Display vars in UILabels - iphone

I'm getting some data from a XML file on my server,here's the parse of the file:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"ipayregs"] ) {
msgAdded = [[attributeDict objectForKey:#"added"] retain];
msgId = [[attributeDict objectForKey:#"id"] intValue];
msgNome = [[NSMutableString alloc] init];
msgName = [[NSMutableString alloc] init];
msgMatricula = [[NSMutableString alloc] init];
msgSenderCode = [[NSMutableString alloc] init];
inNome = NO;
inName = NO;
inMatricula = NO;
inSenderCode = NO;
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = YES; }
if ( [elementName isEqualToString:#"nome"] ) { inName = YES; }
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = YES; }
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = YES; }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( inNome ) { [msgNome appendString:string]; }
if ( inName ) { [msgName appendString:string]; }
if ( inMatricula ) { [msgMatricula appendString:string]; }
if ( inSenderCode ) { [msgSenderCode appendString:string]; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"ipayregs"] ) {
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,#"added",msgNome,#"nome",msgMatricula,#"matricula",msgSenderCode,#"sendercode",msgName,#"name",nil]];
[[messages reverseObjectEnumerator] allObjects];
lastId = msgId;
[msgAdded release];
[msgNome release];
[msgName release];
[msgMatricula release];
[msgSenderCode release];
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = NO;}
if ( [elementName isEqualToString:#"name"] ) { inName = NO;}
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = NO;}
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = NO;}
}
As you can notice,the data downloaded is stored in vars(#"nome",#"matricula"..).But how can i display the values in UILabels,like | label.text = #"nome" |?

it looks like the data is actually being stored in msgNome. #"nome" is a string expression, not an instance variable. So, you would set label.text = msgNome;
also, you should just have:
inNome = [elementName isEqualToString:#"nome"];
instead of all the extra if statements.

Related

Fetching child tag from multi level xml in Objective C

My XML
<MemberBookDTO>
<BookKey>abc</BookKey>
<OrderLineKey>5f8-</OrderLineKey>
<ValidationHash>512D0</ValidationHash>
<BookId>101</BookId>
<Name>Dönüş</Name>
<Author>Abc</Author>
<Narrator>Apn</Narrator>
<OrderDateAsText>01.01.2011 15:00</OrderDateAsText>
<Description>abcd123456.</Description>
<TotalDurationAsSec>0</TotalDurationAsSec>
<TotalSizeAsByte>658943266</TotalSizeAsByte>
<ListImageURL>http://abc.com/SeslenenKitap/File/Picture/abc.png</ListImageURL>
<DetailImageURL>http://abc.com/SKitap/File/Picture/abc.png</DetailImageURL>
<PlayerImageURL>http://abc.com/SKitap/File/Picture/abc.png</PlayerImageURL>
<ImageSign>63514212345693424</ImageSign>
<Chapters>
<ChapterDTO>
<Id>350</Id>
<Name>Bolum 1 - Kaybolmak</Name>
<Sort>1</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/61efdf42-ce30-4c52-aa4f-84748fa28343.ses</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>36</Id>
<Name>Bolum 2 - Kacis</Name>
<Sort>2</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://abc.com/Kitap/File/Audio/730a25a9-a01f-41ee-acda-3c23edbe078a.mp3</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>37</Id>
<Name>Bölüm 3 - Karşılaşma</Name>
<Sort>3</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/7d029835-99a6-4c24-b9f8-735d1496be2e.ses</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>38</Id>
<Name>Bolum 4 - Kavusma</Name>
<Sort>4</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/f9e7535f-5ffb-4be8-ad00-67be3906bffe.ses</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>39</Id>
<Name>Bolum 5 - Yuzlesme</Name>
<Sort>5</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/ae4cdab9-7fdc-4c9b-b2b7-cec7299d2353.ses</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>40</Id>
<Name>Bolum 6 - Itiraf</Name>
<Sort>6</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/60bfc2f9-9fe0-4c90-bdd0-c6253bca7056.ses</DownloadURL>
</ChapterDTO>
<ChapterDTO>
<Id>41</Id>
<Name>Bolum 7 - Donus</Name>
<Sort>7</Sort>
<DurationAsSec>0</DurationAsSec>
<DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/45b31c6a-9b38-4b00-91f1-4d9153dff138.mp3</DownloadURL>
</ChapterDTO>
</Chapters>
</MemberBookDTO>
Objective C Code:
-(id) loadXMLByURL:(NSString *)urlString
{
bookListArray= [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
parser= [[NSXMLParser alloc] initWithContentsOfURL:url];
parser.delegate=self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"MemberBookDTO"])
{
currentBook = [[BookListInfo alloc] init];
currentNodeContentBook=[[NSMutableString alloc] init];
}
else if ([elementname isEqualToString:#"Chapters"])
{
//currentBook = [[BookListInfo alloc] init];
currentNodeContentChapters=[[NSMutableString alloc] init];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// bookKey,orderLineKey,bookAuthor,bookDescription,bookDuration,bookId,bookName,bookNarrator,bookOrderDate,bookSizeAtByte,listImageUrl,detailImageUrl,playerImageUrl,imageSign;
if ([elementname isEqualToString:#"BookKey"])
{
currentBook.bookKey = currentNodeContentBook;
// NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"OrderLineKey"])
{
currentBook.orderLineKey = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"ValidationHash"])
{
currentBook.validationKey = currentNodeContentBook;
// NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"BookId"])
{
currentBook.bookId = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"Name"])
{
currentBook.bookName = currentNodeContentBook;
// NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"Author"])
{
currentBook.bookAuthor = currentNodeContentBook;
// NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"Narrator"])
{
currentBook.bookNarrator = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"OrderDateAsText"])
{
currentBook.bookOrderDate = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"Description"])
{
currentBook.bookDescription = currentNodeContentBook;
//NSLog(#"Description: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"TotalDurationAsSec"])
{
currentBook.bookDuration = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"TotalSizeAsByte"])
{
currentBook.bookSizeAtByte = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"ListImageURL"])
{
currentBook.listImageUrl = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"DetailImageURL"])
{
currentBook.detailImageUrl = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"PlayerImageURL"])
{
currentBook.playerImageUrl = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
if ([elementname isEqualToString:#"ImageSign"])
{
currentBook.imageSign = currentNodeContentBook;
//NSLog(#"currentNodeContent: %#",currentNodeContent);
}
else if ([elementname isEqualToString:#"Chapters"])
{
if ([elementname isEqualToString:#"Id"])
{
currentBook.chapterId = currentNodeContentChapters;
NSLog(#"chapterId: %#",currentNodeContentChapters);
NSLog(#"___________________________________");
}
if ([elementname isEqualToString:#"Name"])
{
currentBook.chapterName = currentNodeContentChapters;
NSLog(#"chapterName: %#",currentNodeContentChapters);
NSLog(#"___________________________________");
}
if ([elementname isEqualToString:#"Sort"])
{
currentBook.chapterSort = currentNodeContentChapters;
NSLog(#"chapterSort: %#",currentNodeContentChapters);
NSLog(#"___________________________________");
}
if ([elementname isEqualToString:#"DurationAsSec"])
{
currentBook.chapterDurationAsSec = currentNodeContentChapters;
NSLog(#"DurationAsSec: %#",currentNodeContentChapters);
NSLog(#"___________________________________");
}
if ([elementname isEqualToString:#"DownloadURL"])
{
currentBook.chapterDownloadUrl = currentNodeContentChapters;
NSLog(#"chapterDownloadUrl: %#",currentNodeContentChapters);
NSLog(#"___________________________________");
}
}
if ([elementname isEqualToString:#"MemberBookDTO"])
{
[bookListArray addObject:currentBook];
currentBook = nil;
currentNodeContentBook = nil;
}
else if ([elementname isEqualToString:#"Chapters"])
{
currentNodeContentChapters=nil;
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
*currentNodeContentBook = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
currentNodeContentChapters = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];*
}
#end
I want to parser both the parent and child tag contents. But The Child tag contents are are found null. I looked various posts and tutorials but undone.Can any one help me please
Add an Log on the didStartElement method and see if all the tags are logged.It will be listed properly and you have to extract it in the rightway
NSLog(#"\n%#", elementname);
See how the parser works first adding this tag.It is not in the parent child way the parser parse the data.It is just First come first serve way the values approch aand you have to write the logic supporting that
For more information on parsing Read this document by apple

Not getting data after XML Parser

I am using XML parser to get my data.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ( [elementName isEqualToString:#"country"] )
{
x++;
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:Id,#"ID",Name,#"NAME",Image,#"IMAGE",nil]];
//**111**
NSLog(#"Data in Message : %#", [messages objectAtIndex:(x-1)]);
[Id setString:#""];
[Name setString:#""];
[Image setString:#""];
}
if ( [elementName isEqualToString:#"id"] )
{
[Id appendString:currentNodeContent];
}
if ( [elementName isEqualToString:#"name"] )
{
[Name appendString:currentNodeContent];
}
if ( [elementName isEqualToString:#"im"] )
{
[Image appendString:currentNodeContent];
}
}
//At //*111* the message is printed but when I do this after calling the xml parser, it doesn't print:
chatParser = [[NSXMLParser alloc] initWithData:receivedData];
[chatParser setDelegate:self];
[chatParser parse];
NSLog(#"\n\nMessages...\n\n%#", messages); //here there is no data printing
at this point it is printing empty..
Messages.
(
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
)
I have define message properties and also sythesize it.
In viewDidLoad I have did this
messages=[[NSMutableArray alloc] init];
Thank you for help..
Your problem is that you use same objects Id, Name, Image all the time and with the lines
[Id setString:#""];
[Name setString:#""];
[Image setString:#""];
set their values to "" at the end of each country element.
It better should look like this (not tested, maybe some typos):
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"id"] )
{
[lastMessage setObject:currentNodeContent forKey:"ID"];
}
else if ( [elementName isEqualToString:#"name"] )
{
[lastMessage setObject:currentNodeContent forKey:"NAME"];
}
else if ( [elementName isEqualToString:#"im"] )
{
[lastMessage setObject:currentNodeContent forKey:"IMAGE"];
}
else if ( [elementName isEqualToString:#"country"] )
{
NSLog(#"Data in Message : %#", lastObject);
[messages addObject:lastMessage];
[lastMessage release];
}
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elName namespaceURI:(NSString *)uri
{
if ( [elementName isEqualToString:#"country"] )
{
lastMessage = [[NSMutableDictionary alloc]init];
}
}
Use the Below Code.Where Table is NSObject Class with the CaseId,PartyId,CartId as a properties in this class.If you have the xml url just called loadXMLByURL method with URl.After parsing you will get Each Object in TableArray which have the Table object with above properties.
NSMutableString *currentNodeContent;
NSXMLParser *parser;
Tweet *currentTweet;
bool isStatus;
-(id) loadXMLByURL:(NSString *)urlString
{
_tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"Table"])
{
currentTable = [Table alloc];
isStatus = YES;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:#"CaseId"])
{
currentTable.CaseId = currentNodeContent;
}
if ([elementname isEqualToString:#"PartyId"])
{
currentTable.PartyId = currentNodeContent;
}
if ([elementname isEqualToString:#"CartId"])
{
currentTable.CartId = currentNodeContent;
}
}
if ([elementname isEqualToString:#"Table"])
{
[self.tableArray addObject:currentTable];
currentTable = nil;
currentNodeContent = nil;
}
}
Let me know if you have any doubt.

Parsing XML in my iPhone application

I parsing some elements from a xml file located on my server to my app.This is the code to parse all elements:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"ipayregs"] ) {
msgAdded = [[attributeDict objectForKey:#"added"] retain];
msgId = [[attributeDict objectForKey:#"id"] intValue];
msgNome = [[NSMutableString alloc] init];
msgName = [[NSMutableString alloc] init];
msgMatricula = [[NSMutableString alloc] init];
msgSenderCode = [[NSMutableString alloc] init];
inNome = NO;
inName = NO;
inMatricula = NO;
inSenderCode = NO;
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = YES; }
if ( [elementName isEqualToString:#"nome"] ) { inName = YES; }
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = YES; }
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = YES; }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( inNome ) { [msgNome appendString:string]; }
if ( inName ) { [msgName appendString:string]; }
if ( inMatricula ) { [msgMatricula appendString:string]; }
if ( inSenderCode ) { [msgSenderCode appendString:string]; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"ipayregs"] ) {
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,#"added",msgNome,#"nome",msgMatricula,#"matricula",msgSenderCode,#"sendercode",msgName,#"name",nil]];
[[messages reverseObjectEnumerator] allObjects];
lastId = msgId;
[msgAdded release];
[msgNome release];
[msgName release];
[msgMatricula release];
[msgSenderCode release];
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = NO;}
if ( [elementName isEqualToString:#"name"] ) { inName = NO;}
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = NO;}
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = NO;}
}
As you can see all data is placed in a array to be displayed after on a UITableView,using this code:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return ( messages == nil ) ? 0 : [messages count];
}
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"newsCustomCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"newsCustomCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *timeDate = (UILabel *)[cell viewWithTag:1];
timeDate.text = [itemAtIndex objectForKey:#"added"];
UILabel *userL = (UILabel *)[cell viewWithTag:2];
userL.text = [itemAtIndex objectForKey:#"nome"];
UILabel *textL2 = (UILabel *)[cell viewWithTag:3];
textL2.text = [itemAtIndex objectForKey:#"matricula"];
UILabel *textL3 = (UILabel *)[cell viewWithTag:4];
textL3.text = [itemAtIndex objectForKey:#"sendercode"];
return cell;
}
Ok,now is my problem,how can i make a UILabel display the value of the strings,per example the #"nome",be displayed in a UILabel when the view loads.It's related to this piece of code:
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
But i don't know how i can set it up.

iphone using NSXML parser how to parse the attributes

I want to know how to parse the attributes in below xml file
<ROOT_ELEMENT><RESPONSE READ_TAG="LEVEL_LIST" RESULT="" TEXT=""/><USER USER_NAME="newadmin01" TOKEN_ID="0.766003221016982" FULL_NAME="newadmin01, newadmin01"/><DATETIME UNFORMATTED_TEXT="Aug 10 2011 12:25PM" FORMATTED_TEXT="10 Aug 12:25"/><BREADCRUMB/><LEVEL_LIST><LEVEL ID="4519" NAME="Mega Mart" CHILD_EXISTS="Y" ADD_EDIT_PRIVILEGE="Y"/></LEVEL_LIST></ROOT_ELEMENT>
here it is my parser code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
elemName = [[NSString alloc] initWithString:elementName];
NSLog(#"element Name = %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([elemName isEqualToString:#"RESPONSE"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:#"USER"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
if ([elemName isEqualToString:#"DATETIME"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:#"BREADCRUMB"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:#"LEVEL"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elemName isEqualToString:#"RESPONSE"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:#"USER"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:#"DATETIME"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:#"BREADCRUMB"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:#"LEVEL"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
}
Look at the NSXMLParserDelegate's message declaration
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
It has an attributeDict for the element you currently parse.
Iterate over the key-value pairs and deal with them as you like.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
elemName = [[NSString alloc] initWithString:elementName];
NSLog(#"element Name = %#", elementName);
NSEnumerator *keyEnumerator = attributeDict.keyEnumerator()
// for each element the parser encounters:
// create a new dictionary to hold the attributes
NSMutableDictionary *myAttributes = [[NSMutableDictionary alloc] init];
// go through each attribute for the elementName tag
while ( (id aKey = [keyEnumerator nextObject]) )
// and store the attribute to the dictionary by copying over the values
[myAttributes setObject:[attributesDict objectForKey:aKey] forKey:aKey];
// after we caught all attributes and copied them into our own data structure
// put them into an instance dicionary to get a structure which holds
// all attributes for any element tag we encounter
// accessible by the elementName as key
[self.attributesByElementDictionary setObject:myAttributes forKey:elementName];
[myAttributes release];
}
Practically this would produce a dictionary for the - example! - RESPONSE tag:
The dictionary would look like:
{
"READ_TAG": "LEVEL_LIST",
"RESULT": "",
"TEXT": ""
}
For a more general solution you would not use a flat dictionary but rather form some kind of tree structure. This is a little bit more work and more robust - but this little example should give you the idea to extract the attributes from a tag and process them afterwards

Display dynamic data in UIPickerView in iPhone

I am using NSXMLParsing and UIPickerView in my application but i am not able to display
data in uipicker view after parsing. can anyone help me out about this problem.
I just want to know that how to store data in array and display it in uipickerview.
thnx frnds
Thanks mahboudz... I have done that what u said but its not working.. actually i have parsed some data and stored it in nsmutablearray using dictionary... and want to display it in uipicker view but its not working.. my code is:-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"news"] ) {
//msgAdded = [[attributeDict objectForKey:#"added"] retain];//fetch session from message tag.
//msgId = [[attributeDict objectForKey:#"id"] intValue];//fetch id from message tag.
newId = [[NSMutableString alloc] init];
newsTitle =[[NSMutableString alloc]init];
newsOverview = [[NSMutableString alloc]init];
newsOfDay = [[NSMutableString alloc] init];
createdOn = [[NSMutableString alloc]init];
newsDescription = [[NSMutableString alloc]init];
idNews = NO;
ttlNews = NO;
overvwNews = NO;
dyNews = NO;
onCreated = NO;
desNews = NO;
//inFromUserName = NO;
}
if ( [elementName isEqualToString:#"news_id"] ) {
idNews = YES;
}
if ( [elementName isEqualToString:#"news_title"] ) {
ttlNews = YES;
}
if ( [elementName isEqualToString:#"news_overview"] ) {
overvwNews = YES;
}
if ( [elementName isEqualToString:#"is_news_of_the_day"] ) {
dyNews = YES;
}
if ( [elementName isEqualToString:#"created_on"] ) {
onCreated = YES;
}
if ( [elementName isEqualToString:#"news_description"] ) {
desNews = YES;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( idNews ) {
[newId appendString:string];
}
if (ttlNews) {
[newsTitle appendString:string];
}
if ( overvwNews ) {
[newsOverview appendString:string];
//NSLog(#"USER_NAME ===== %d",fromUserName);
}
if (dyNews) {
[newsOfDay appendString:string];
}
if (onCreated) {
[createdOn appendString:string];
}
if (desNews) {
[newsDescription appendString:string];
// NSLog(#"MESSAGE ==== %#",chatComment);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"news"] ) {
//[news addObject:[NSDictionary dictionaryWithObjectsAndKeys:chatSession,#"chatSession",fromUserName,#"fromUserName",chatComment,#"chatComment",nil]];
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:newsTitle,#"title",newId,#"id",nil]];
}
if ( [elementName isEqualToString:#"news_id"] ) {
idNews = NO;
}
if ( [elementName isEqualToString:#"news_title"] ) {
ttlNews = NO;
}
if ( [elementName isEqualToString:#"news_overview"] ) {
overvwNews = NO;
}
if ( [elementName isEqualToString:#"is_news_of_the_day"] ) {
dyNews = NO;
}
if ( [elementName isEqualToString:#"created_on"] ) {
onCreated = NO;
}
if ( [elementName isEqualToString:#"news_description"] ) {
desNews = NO;
}
}
and now i want to display the contents of array in uipicker view. that is objectForKey:#"newstitle"
i have tried it display through following code:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *partyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
partyLabel.text= [[messages objectAtIndex:row]objectForKey:#"title"];
partyLabel.font = [UIFont boldSystemFontOfSize:13];
partyLabel.textAlignment = UITextAlignmentCenter;
partyLabel.backgroundColor = [UIColor clearColor];
return partyLabel;
}
but its not working ...please can any one help me out..
Thnx
Kunal
You are free to store the parsed data in whatever format you wish, such as an NSArray or a C array. When the UIPicker needs to display each row, it will call one of the following, depending on how you are managing the Picker:
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
You need to implement only one of the two. The first one is easiest. When you get called for each row, then just return the NSString that you would like to have displayed in that row. The second one is more complex, but allows you to have more flexibility such as formatting the row or adding images.
If you are changing the display data after the UIPicker is on screen, you will need to call reloadComponent: or reloadAllComponents to update the display.
I think that at first you have to parse the xml and store data into an nsmutable array, then you have to display data in picker view from your array