1 Replies - 2071 Views - Last Post: 24 June 2012 - 02:44 PM

#1 soulslicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-April 12

XML Parsing Help

Posted 30 May 2012 - 05:29 AM

Okay, so let me put this straight out. I have the following xml code

<?xml version="1.0" encoding="UTF-8"?>
<data>
	<loc>
		<Title>AS1</Title>
		<Latitude>1.295182</Latitude>
		<Longitude>103.771904</Longitude>
	</loc>
	<loc>
		<Title>AS2</Title>
		<Latitude>1.295171</Latitude>
		<Longitude>103.771276</Longitude>
	</loc>


And, I wish to add the content into an object/class (OBJECT NUS) that has title, and Coordinate2d object.

1. In the following code below, I am able to pick up the terms title, latitude and longitude, unfortunately, in the Title part, it never picks up the content of the title (example AS1) Is my code wrong?

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    if([elementName isEqualToString:@"loc"]){
        self.location=[[NUSLocations alloc]init];
    }
    
    if([elementName isEqualToString:@"Title"]){
        //NSString* st=[[attributeDict valueForKey:@"Title"]string];
        //NSLog(@"%@",st);
    }
    
}


2. However, the following code below, picks up the contents of the stuff, but in a straight order.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"%@",string);
}


3. Then there seems to be another chunk of code I must add, called didEndElement as such, I have no idea what it does
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName


Can I know the correct procedure on how to actually parse the above data properly. From what I have read in the tutorial,
didStartElement is for initializing one object, in my case the data between <loc>
parser foundCharacters is to put the contents into the object
and didEndElement is to add the object to the array

I have no idea how to do this, could someone point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: XML Parsing Help

#2 Arappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-September 09

Re: XML Parsing Help

Posted 24 June 2012 - 02:44 PM

View Postsoulslicer, on 30 May 2012 - 06:29 AM, said:

Okay, so let me put this straight out. I have the following xml code

<?xml version="1.0" encoding="UTF-8"?>
<data>
	<loc>
		<Title>AS1</Title>
		<Latitude>1.295182</Latitude>
		<Longitude>103.771904</Longitude>
	</loc>
	<loc>
		<Title>AS2</Title>
		<Latitude>1.295171</Latitude>
		<Longitude>103.771276</Longitude>
	</loc>


And, I wish to add the content into an object/class (OBJECT NUS) that has title, and Coordinate2d object.

1. In the following code below, I am able to pick up the terms title, latitude and longitude, unfortunately, in the Title part, it never picks up the content of the title (example AS1) Is my code wrong?

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    if([elementName isEqualToString:@"loc"]){
        self.location=[[NUSLocations alloc]init];
    }
    
    if([elementName isEqualToString:@"Title"]){
        //NSString* st=[[attributeDict valueForKey:@"Title"]string];
        //NSLog(@"%@",st);
    }
    
}


2. However, the following code below, picks up the contents of the stuff, but in a straight order.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"%@",string);
}


3. Then there seems to be another chunk of code I must add, called didEndElement as such, I have no idea what it does
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName


Can I know the correct procedure on how to actually parse the above data properly. From what I have read in the tutorial,
didStartElement is for initializing one object, in my case the data between <loc>
parser foundCharacters is to put the contents into the object
and didEndElement is to add the object to the array

I have no idea how to do this, could someone point me in the right direction?


I find AFNetworking (https://github.com/AFNetworking/AFNetworking) to be a much easier and feature rich alternative than NSXMLParser. Maybe you should take a look if you never have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1