Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 04-28-2009, 11:05 PM   #59 (permalink)
Topper
New Member
 
Join Date: Apr 2009
Posts: 2
Default Parser issue

Hello everyone

For my iphone application I need to request data about an object through a webservice.
I've used the code provided in this thread to do basic GET Request.
In fact, for now on, it just request an XML file located on a webserver (http://.../lait.xml).

Here's the XML i'm supposed to parse :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<object>
	<id>126</id> <!-- The object desserved has the id 126 in this kwys instance -->
	<name>Milk</name> <!-- It is codenamed Milk -->
	<information lang="fr"> <!-- We asked for french information -->
		<title>Le lait</title> <!-- The title is the localized object's name -->
		<!-- Here be the 3 types of content (for now): text, media, and link. -->
		<!-- Each one may appear as many time as possible, in any order. -->
		<text> <!-- A text entry is a caption followed by contents -->
			<caption>Introduction</caption>
			<!-- There must be at least one content Element. -->
			<content>
				Le lait est une boisson de couleur généralement blanchâtre
				produite par
				les mammifères femelles (y compris les monotrèmes).
				Cette capacité
				des femelles est une des caractéristiques définissant
				les mammifères.
				Le lait est produit par les cellules sécrétrices des
				glandes
				contenues dans les mamelles. Le lait sécrété dans les
				premiers jours
				après la parturition s'appelle le colostrum.
			</content>
			<content>
				La fonction première du lait est de nourrir la progéniture
				jusqu'à ce
				qu'elle soit sevrée, c'est-à-dire capable de digérer
				d'autres
				aliments. Dans la plupart des civilisations humaines, le
				lait des
				animaux (eux-mêmes mammifères) domestiques (vache, brebis,
				chèvre,
				jument, dri, chamelle, dromadaire, bufflonne) est couramment
				consommé.
			</content>
			<content>
				Par analogie, on utilise également le terme de lait pour
				désigner plusieurs boissons de consistance et/ou d'apparence
				similaires et produites à base de végétaux, comme le lait de soja,
				de coco, de riz, d'amande, d'avoine ou encore de pistache.
			</content>
		</text>
		<media> <!-- Media refers to static media (picture) or dynamic/rich (video, ...). -->
			<caption>Une fille pas douée en train de tout foutre à coté.</caption>
			<href>http://upload.wikimedia.org/wikipedia/commons/5/59/MilkMaid.JPG</href>
		</media>
		<link> <!-- A link may refer to an external source, like this french wikipedia article. -->
			<caption>Le lait sur wikipedia.</caption>
			<href>http://fr.wikipedia.org/wiki/Lait</href>
		</link>
		<link> <!-- A link may also refer to another internal object, like this milk box. -->
			<caption>La brique de lait.</caption>
			<id>956</id>
		</link>
	</information>
</object>
The webrequest seems ok and here's the result from the NSDictionnary after going through server:

Code:
Response : {
    object =     {
        id = 126;
        information =         {
            link =             {
                caption = "La brique de lait.";
                id = 956;
            };
            media =             {
                caption = "\U00e9e en train de tout foutre \U00e0 cot\U00e9.";
                href = "http://upload.wikimedia.org/wikipedia/commons/5/59/MilkMaid.JPG";
            };
            text =             {
                caption = Introduction;
                content = "\U00e9galement le terme de lait pour\n\t\t\t\td\U00e9signer plusieurs boissons de consistance et/ou d'apparence\n\t\t\t\tsimilaires et produites \U00e0 base de v\U00e9g\U00e9taux, comme le lait de soja,\n\t\t\t\tde coco, de riz, d'amande, d'avoine ou encore de pistache.\n\t\t\t";
            };
            title = "Le lait";
        };
        name = Milk;
    };
}
The only thing is that the XMLParser seems to skip every node type he has already been going through (like <content> for example).

Could anyone tell me if there's a modification to do in the parser to correct that issue ?
I repost the parser .m file:
Code:
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	result=[[NSMutableDictionary alloc] init];
	parentArray=[[NSMutableArray alloc] init];
	[parentArray addObject:result];
	currentElementName=@"";
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if (qName) {
		elementName = qName;
	}
	currentElementValue=@"";
	if (currentElementName!=@"")
	{
		id newParent=NULL;
		if ([currentElementName isEqualToString:@"*Array*"])
		{
			newParent=[[NSMutableArray alloc] init];
		} else {
			newParent=[[NSMutableDictionary alloc] init];
		}
		[parentArray addObject:newParent];
	}
	currentElementName=elementName;
	NSLog(@"Name %@", currentElementName); //to check the parser is really going through every node received from the web and it does. It just doesn't store 2 similar type of node.
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
	if (qName) {
		elementName = qName;
	}
	
	if (currentElementName==@"")
	{
		//We're adding a container with children.  Add it to the parent and remove this item fromt he parentArray
		int currentIndex=[parentArray count]-1;
		int parentIndex=currentIndex - 1;
		id currentChild=[parentArray objectAtIndex:currentIndex];
		id currentParent=[parentArray objectAtIndex:parentIndex];
		
		if ([currentParent isKindOfClass:[NSMutableArray class]])
		{
			[currentParent addObject:currentChild];
		} else {
			[currentParent setObject:currentChild forKey:elementName];
		}
		
		[parentArray removeObjectAtIndex:currentIndex];
	} else {
		//We're adding a simple type element
		int currentIndex=[parentArray count]-1;
		id currentParent=[parentArray objectAtIndex:currentIndex];
		if ([currentParent isKindOfClass:[NSMutableArray class]])
		{
			[currentParent addObject:currentElementValue];
		} else {
			[currentParent setObject:currentElementValue forKey:currentElementName];
		}
	}
	currentElementName=@"";
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	currentElementValue=string;
}
Hope someone can help. I just want a generic xml parser which gives me back a big NSDictionnary i can go through and get data.

Thanks
Topper is offline   Reply With Quote
 
Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Online Users: 299
22 members and 277 guests
Aaronkwok963, alexy, bensj, bravetarget, BSDimwit, Chilibird, CHV, dre, Gudus, jamison, KyleTomson, LemonMeringue, mer, punknine, ronm3xico, sindhutiwari, SirGeraint, smasher, VMan, ybrikeeg, zhz, _nivek
Most users ever online was 779, 05-11-2009 at 09:55 AM.
» Stats
Members: 24,225
Threads: 39,002
Posts: 171,083
Top Poster: smasher (2,570)
Welcome to our newest member, Aaronkwok963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.