NSxml parser upto 3 levels on iphone - iphone

i have a issue getting the value of A->1,2,3,4 & b->1,2,3,4
i have to show these data on 3 tables
table 1) A , B
table 2)when user click on A -> he can see 1,2,3,4 in tablular form
but i my case i can only show the last value of A i.e 4 ( not 1,2,3)
how to parse all 1,2,3,4
<?xml version="1.0" encoding="UTF-8"?>
<hall>
<movie Name="A">
<stall Name="1">
<description>qwe.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4i">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie >
<movie Name="B">
<stall Name="t1">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie>
</hall>
#
UPDATES
#
my problem is i dont know how to separate Movie name A and B data
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init];
appDelegate.books = [[NSMutableArray alloc]init];
appDelegate.HospN = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:#"movie"])
{
currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ;
[appDelegate.books addObject:[attributeDict objectForKey:#"Name"]];
//[aBook.movie addObject:[attributeDict objectForKey:#"Name"]];
NSLog(#"Processing Value: %#", currentMovie);
//aBook.testament=currentMovie;
aBook.stall = [attributeDict objectForKey:#"name"];
NSLog(#"Movie A name : %#",[movieList objectForKey:#"A"]);
}
else if([elementName isEqualToString:#"stall"])
{
[appDelegate.HospN addObject:[attributeDict objectForKey:#"Name"]];
NSLog(#"Processing stall Value: %#", currentElementValue);
NSLog(#"appDelegate.HospN: %#", appDelegate.HospN);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
//NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"hall"])
{
//NSLog(#"Movie List : %#",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:appDelegate.books forKey:currentMovie];
NSLog(#"Movie name : %#",appDelegate.books );
NSLog(#"Movie A name : %#",[movieList objectForKey:#"A"]);
[currentMovie release];
//[stallNames release];
currentMovie = nil;
//stallNames = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
my array my appDelegate.HospN: value is (
1,
2,
3,
4i,
t1,
2,
3,
4
)
**but i want to chop data at A= 1,2,3,4i
and B= t1,2,3,4**
Updtate
how to get the value of description and rating ??
else if([elementName isEqualToString:#"description"])
{
NSLog(#"desp List : \n%#\n",currentElementValue);// i can see all value but how to chop data based on A and B
[movieList setObject:currentLink forKey:#"description"];
[appDelegate.despArray addObject:[movieList copy]];
NSLog(#"adding story: %#",currentLink);
}
else if([elementName isEqualToString:#"rating"])
{
}

By observing the xml you will find the relationship like this...
A hall can have one or more movie and Each movies can have one ore more stall.
so what you can do is.
have a instance dictionary, say movieList, and an instance array say,stallNames, and one movieName,NSString in your xml parsing class.
movieList : will have mapping of movie name with their respective stalls.(NSMutableDictionary)
stallNames : will contain stalls related to particular movie.(NSMutablArray)
currentMovie: will have current movie name as it would be used as key in movieList Dictionary.(NSString)
Now in startElement method ...
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init];
}
else if([elementName isEqualToString:#"movie"])
{
currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ;
stallNames = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:#"stall"])
{
[stallNames addObject:[attributeDict objectForKey:#"Name"]];
}
in didEndElement method
if([elementName isEqualToString:#"hall"])
{
NSLog(#"Movie List : %#",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:stallNames forKey:currentMovie];
[currentMovie release];
[stallNames release];
currentMovie = nil;
stallNames = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
Note : as in above code posted by you , you have a Book class, and it seems it represents Movie, so what you can do is, have another property which is array type which will hold stall names. and this Book object will be added later on an array(in didEndElement when you will encounter elementName "movie"). make this array as instance variable and initialize it when you encounter "hall" tag in startElement.
UPDATE ANS(AS PER NEW CODE)
//declare following instance variables. I am leaving aBook variable because I am not sure of its use.
NSMutableDictionary *movieList;
NSMutableArray *stallArray;
NSMutableString *currentMovieKey;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"hall"])
{
movieList = [[NSMutableDictionary alloc]init]; // it will contain list of all the movies in the xml.
}
else if([elementName isEqualToString:#"movie"])
{
currentMovieKey = [[NSMutableString alloc] initWithString:[attributeDict objectForKey:#"Name"]] ; // will be used for setting key for stallArray.
stallArray= [[NSMutableArray alloc] init]; // it will contain stall names.
}
else if([elementName isEqualToString:#"stall"])
{
[stallArray addObject:[attributeDict objectForKey:#"Name"]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"hall"])
{
//parsing finished...end of root tag. movieList will be the final dictionary that will have your data
NSLog(#"Movie List : \n%#\n",movieList);
}
else if([elementName isEqualToString:#"movie"])
{
[movieList setObject:stallArray forKey:currentMovieKey];
[currentMovieKey release];
currentMovie = nil;
[stallArray release];
stallArray = nil;
}
else if([elementName isEqualToString:#"stall"])
{
//you can populate other attributes here if stall is not just string but a modal class.
}
//I was not sure about your else part...
[currentElementValue release];
currentElementValue = nil;
}
Thanks,

Related

Parse 2 RSS feeds in app delegate

I'm following a tutorial and it's working fine but if I wanted to parse 2 RSS feeds it seems to overwrite one array instead of saving them in the respective arrays.
This is in my delegate:
NSURL *url2 = [[NSURL alloc] initWithString:#"myRSSFEED1"];
NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithContentsOfURL:url2];
//Initialize the delegate.
XMLParser1 *parser1 = [[XMLParser1 alloc] initXMLParser];
//Set delegate
[xmlParser1 setDelegate:parser1];
//Start parsing the XML file.
BOOL successs = [xmlParser1 parse];
if(successs)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
//VIDS
NSURL *url = [[NSURL alloc] initWithString:#"MYRSSFEED2"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
that parses feed 1, allocates it to the array then parses 2 and seems to overwrite the first insteaf of using the second array that are defined as
#synthesize pics;
#synthesize books;
And saved in my XMLParser & XMLParser1
I can't figure out how to stop it from overwriting.
Here is my XMLParsers too:
- (void)parsers:(NSXMLParser *)parsers didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate2.pics = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
apics = [[BookPhoto alloc] init];
//Extract the attribute here.
apics.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading HAVid value :%i", apics.bookID);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parsers:(NSXMLParser *)parsers didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate2.pics addObject:apics];
[apics release];
apics = nil;
}
else
[apics setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
And my XMLParser.m
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading id value :%i", aBook.bookID);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
Any help on this would be brilliant,
I suggest putting a breakpoint at the point where the array is not getting loaded, and seeing if (1) it even gets called, and (2), whether it is saving the data into the array at all.
my bad
seems i had extra s's in parser:
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
should have been:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
cheers for the help tho

iPhone XML Parsing problem

I am trying to extract the title from this xml:
<entry>
...
<title type="html">Some title</title>
<published>2011-02-07T00:04:16Z</published>
<updated>2011-02-07T00:04:16Z</updated>
...
</entry>
in the NSXMLParsing's didStartElement code:
if ([elementName isEqualToString:#"entry"])
{
item = [[NSMutableDictionary alloc] init];
}
if([elementName isEqualToString:#"title"])
{
currentElement = [elementName copy];
currentTitle = [[NSMutableString alloc] init];
}
in foundCharacters:
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
in didEndElement:
if ([elementName isEqualToString:#"entry"])
{
[item setObject:currentTitle forKey:#"title"];
[currentElement release];
currentElement = nil;
[item release];
item = nil;
}
The problem is that for some reason when it gets to the didEndElement the currentTitle has got the three node's content, so its:
Some
title2011-02-07T00:04:16Z2011-02-07T00:04:16Z
I don't get why its picking up the published and updated node and appending them to the title string.
You set currentElement to #"title" in didStartElement: but you never unset it. The first element in this particular XML file is title, so you set currentElement and for every foundCharacters: thereafter you append them. Change didStartElement: to:
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"])
{
item = [[NSMutableDictionary alloc] init];
}
if([elementName isEqualToString:#"title"])
{
currentTitle = [[NSMutableString alloc] init];
}
You have to add to didEndElement:
if ([elementName isEqualToString:#"title"])
{
[currentElement release];
currentElement = nil;
}
define
NSMutableString *strFoundCharacters; in class method set property and synthesize it.
then in
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
NSString *strAppend = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([strAppend length] > 0) {
[self.strFoundCharacters appendString:string];
}
}
make nil to strfoundcharacter in
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"title"] ) {
self.currentTitle = self.strFoundCharacters;
}
[self.strFoundCharacters setString:#""];
}
i think it will work

Using NSXMLParser with CDATA

I'm parsing an RSS feed with NSXMLParser and it's working fine for the title and other strings but one of the elements is an image thats like
<!CDATA <a href="http:image..etc>
How do I add that as my cell image in my table view? Would I define that as an image type?
This is what i'm using to do my parsing:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [ [NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
NSLog(#"adding story: %#", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
}
You can use
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
The point of CDATA is that anything within it is not treated as part of the XML document.
So if you have tags in CDATA the parser will ignore them.
I'm guessing this CDATA is in the description element. So you'll need to extract the tags from the "description" element, either manually or via another instance of a parser.

XMLParser and NSMutableArray

I have followed a tutorial and the XML parsing part is working all right.
My problem is that when printing out the values that I store into the object (in my case the object is a car) always remains null. I save all the objects into an NSMutableArray which always seems to have a length/count == 0...what am I doing wrong? The array is, as can be seen in the code, defined in the delegate class...
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (Car2GoAppDelegate *) [[UIApplication sharedApplication]delegate];
currentElementValue = [[NSMutableString alloc]init];
return self;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attribute:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"kml"]) {
appDelegate.cars = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:#"Placemark"]) {
aCar = [[Car alloc]init];
}
else
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
[currentElementValue appendString:string];
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"kml"])
return;
if([elementName isEqualToString:#"Style"])
return;
if([elementName isEqualToString:#"IconStyle"])
return;
if([elementName isEqualToString:#"Icon"])
return;
if([elementName isEqualToString:#"color"])
return;
if([elementName isEqualToString:#"colorMode"])
return;
if([elementName isEqualToString:#"scale"])
return;
if([elementName isEqualToString:#"href"])
return;
if([elementName isEqualToString:#"styleUrl"])
return;
if([elementName isEqualToString:#"a"])
return;
if([elementName isEqualToString:#"Placemark"]) {
[appDelegate.cars addObject:aCar];
[aCar release];
aCar = nil;
}
else if([elementName isEqualToString:#"name"]) {
[aCar setValue:currentElementValue forKey:elementName];
}
else if([elementName isEqualToString:#"description"]) {
[aCar setValue:currentElementValue forKey:elementName];
[currentElementValue setString:#""];
}
else if([elementName isEqualToString:#"coordinates"]) {
[aCar setValue:currentElementValue forKey:elementName];
[currentElementValue setString:#""];
NSLog(#"aCar coordinates:%#", [aCar coordinates]);
}
else
return;
}
- (void) dealloc {
[aCar release];
[currentElementValue release];
[super dealloc];
}
#end
/loop
Your code looks OK. Are you sure your XML contains element wrapped in Placemark tags??
Also is there more than one kml element in your xml? Could there be one that is empty which is causing your original array to be discarded and replaced with a new one of length 0?
Don't you need to use the initWithCapacity method, in order to initialize the mutableArray properly ?
Instead of using directly appDelegate.cars, try with a local MutableArray, and see if something is different ?

Adding objects to an NSMutableArray, order seems odd when parsing from an XML file

I am parsing an XML file for two elements: "title" and "noType". Once these are parsed, I am adding them to an object called aMaster, an instance of my own Master class that contains NSString variables.
I am then adding these instances to an NSMutableArray on a singleton, in order to call them elsewhere in the program. The problem is that when I call them, they don't seem to be on the same NSMutableArray index... each index contains either the title OR the noType element, when it should be both... can anyone see what I may be doing wrong? Below is the code for the parser. Thanks so much!!
#import "XMLParser.h"
#import "Values.h"
#import "Listing.h"
#import "Master.h"
#implementation XMLParser
#synthesize sharedSingleton, aMaster;
- (XMLParser *) initXMLParser {
[super init];
sharedSingleton = [Values sharedValues];
aMaster = [[Master init] alloc];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
aMaster = [[Master alloc] init];
//Extract the attribute here.
if ([elementName isEqualToString:#"intro"]) {
aMaster.intro = [attributeDict objectForKey:#"enabled"];
} else if ([elementName isEqualToString:#"item"]) {
aMaster.item_type = [attributeDict objectForKey:#"type"];
//NSLog(#"Did find item with type %#", [attributeDict objectForKey:#"type"]);
//NSLog(#"Reading id value :%#", aMaster.item_type);
} else {
//NSLog(#"No known elements");
}
//NSLog(#"Processing Element: %#", elementName); //HERE
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else {
[currentElementValue appendString:string];//[tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
CFStringTrimWhitespace((CFMutableStringRef)currentElementValue);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[sharedSingleton.master addObject:aMaster];
NSLog(#"Added %# and %# to the shared singleton", aMaster.title, aMaster.noType); //Only having one at a time added... don't know why
[aMaster release];
aMaster = nil;
} else if ([elementName isEqualToString:#"title"]) {
[aMaster setValue:currentElementValue forKey:#"title"];
} else if ([elementName isEqualToString:#"noType"]) {
[aMaster setValue:currentElementValue forKey:#"noType"];
//NSLog(#"%# should load into the singleton", aMaster.noType);
}
NSLog(#"delimiter");
NSLog(#"%# should load into the singleton", aMaster.title);
NSLog(#"%# should load into the singleton", aMaster.noType);
[currentElementValue release];
currentElementValue = nil;
}
- (void) dealloc {
[aMaster release];
[currentElementValue release];
[super dealloc];
}
#end
Every time that didStartElement is called, you're setting aMaster to a new instance of the Master class. Based on the implementation of didEndElement, it looks like you should only be creating a new instance whenever a new item tag is found. This could be why each entry in the array has one value or the other, since a new instance is created for each value.