New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
iOS Development Discussion on developing apps for the iOS platform.

NSXML help required


Post Reply New Thread Subscribe

 
Thread Tools
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
Dear All

I want to populate my uitable with data fethced from xml file.
xml file is something like:
Code:
<Main_Project1>
   <Project1>
       <SUb_project1>
                <file>abc.txt</file>
                <file>abc.pdf</file>
      </SUb_project1>
      <SUb_project2>
                 <file>abc.txt</file>
                <file>abc.csv</file>
       </SUb_project2>
    </Project1>
      <Project2>
       <SUb_project1>
                <file>abc.txt</file>
                <file>abc.txt</file>
      </SUb_project1>
      <SUb_project2>
                 <file>abc.txt</file>
                <file>abc.txt</file>
       </SUb_project2>
    </Project2>
.........
.......
</Main_Project1>
I want to parse xml like this so that i get Project name as separate list, sub_project name with separate list and file names as separate list. How to parse xml using NSXML?

Kindly provide me some tutorial with sample code worked on for some sample xml.

Thanks
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
There are many excellent examples and tutorials out there for NSXMLParser... Google is your friend:

NSXMLparser tutorial - Google Search

NSXMLparser example - Google Search

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
Thanks for encouraging me to proceed.
I tried writing code for parsing the xml file, a part of which is like:

Code:
<Project> 
		<subproject> 
			<subprojectname>SUBPPROJECT1 </subprojectname>
					<docid> 
			           	 <name>ColorContourCanvas1.java</name> 
					</docid> 
					<docid> 
			                <name>ColorContourCanvas11.java</name> 
					</docid> 
                </subproject>
                <subproject> 
			<subprojectname>SUBPPROJECT2 </subprojectname>
					<docid> 
			           	 <name>ColorContourCanvas2.java</name> 
					</docid> 
					<docid> 
			                <name>ColorContourCanvas22.java</name> 
					</docid> 
                </subproject>
</Project>
I want to create hash map where each subprojectname will correspond to list of files attached to it.For ex: SUBPROJECT1 is linked to ColorContourCanvas1.java and ColorContourCanvas11.java


I am able to ge the list of subprojectname and docid name but it includes all subprojectname and docid. The code I wrote is:

Code:
- (void)parseXMLFile {
	NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"];
	NSData *data = [NSData dataWithContentsOfFile:path];
	parser = [[NSXMLParser alloc] initWithData:data];
	[parser setDelegate:self];
	[parser parse]; 
	[parser release];
}
and delegates as:

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    currentElement = [elementName copy];
	if ([elementName isEqualToString:@"subproject"]) {
		entry = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSMutableString alloc] init];
		currentContent = [[NSMutableString alloc] init];
    }
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	if ([elementName isEqualToString:@"subproject"]) {
		[entry setObject:currentTitle forKey:@"subproject_name"];
		[entry setObject:currentContent forKey:@"file_name"];
		for ( currentTitle in [entry allKeys]){
			id as = [entry objectForKey:currentTitle];
			NSLog(@" %@", as);
			[stories insertObject:as atIndex:i++];
			
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	
	if ([currentElement isEqualToString:@"subprojectname"]) {
		[currentTitle appendString:string];
	} 
	if ([currentElement isEqualToString:@"name"]) {		
		[currentContent appendString:string];

	}
			
}

But as of now I am getting a list as:

SUBPPROJECT1
ColorContourCanvas1.java ColorContourCanvas11.java
SUBPPROJECT2
ColorContourCanvas2.java
ColorContourCanvas22.java

I am not able to understand how to implement my requirement. Kindly help
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
Well, I guess a good starting place - what did you expect the outcome to be? As you have it written there will be one title for the project and one sub-project name that will be a concatenation of all of the sub-projects.

Also, you've got a fairly nice memory leak in there as the code is currently written.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
Can you state me how to navigate within child node and get the list of sub-child node and then coming back to another child node?
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
How would you want the data stored? For example, would you want:

Object represent a specific project containing a list of sub projects
Object represent a specific sub project containing a list of the file name for the docid?

I'm trying to understand how you want the data represented to help give a better answer.

Usually you create a new variable for each element as its triggered and add it to the appropriate parent variable (whether it be an object or an array, etc.) once the given element has been finished being parsed then release the variable that holds that element.

You on the other hand, allocate all of the memory you think you need at once when the parent element you are looking for is found which may not be good because what happens if you have many sub documents? plus when you reach the end of the element, you don't release any of the allocated memory and re-allocate new memory for those areas when a new element is found so if you have 50 sub projects, you'll have 49 sets of leaked memory as it's coded right now.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
Hi

I want :

Object represent a specific project containing a list of sub projects as well as
Object represent a specific sub project containing a list of the file name for the docid.

With the current code , its not possible.
I was just thinking to start with second requirement and getting list of all file names corresponding to each sub-project name.
But right now I am not getting list like that.
Moreover I am sure there is memory leak, because when i try to add subproject name/file name in some nsmutable array, it says index beyond range and stack becomes overflowed. I am not able to grasp it.
QUOTE Thanks

Post Reply New Thread Subscribe


« NSDictionary iteration | Key- Value Table help required »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Printer - Authentication Required Chris T Switcher Hangout 6 08-10-2012 04:48 AM
Time Machine Size Required? brunel OS X - Operating System 6 06-26-2009 10:28 AM
iWeb Designer Required iwebtemplate Web Design and Hosting 0 01-23-2009 05:03 AM
OBJECTIVE-C / COCOA MacOS Developer HOME BASED required by leading Company (VACANCY) Leigh OS X - Development and Darwin 0 11-26-2008 01:04 PM
Required Thread Reading macAttack Community Suggestions and Feedback 7 04-23-2006 08:48 PM

All times are GMT -4. The time now is 07:18 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?