| iOS Development Discussion on developing apps for the iOS platform. |
| Post Reply | New Thread | Subscribe |
|
|
Thread Tools |
![]() Member Since: Jun 03, 2011
Posts: 54
![]() |
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>
Kindly provide me some tutorial with sample code worked on for some sample xml. Thanks |
| QUOTE Thanks | |
|
Member Since: Feb 25, 2009
Posts: 2,084
![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2012 Non-retina MBP, 2.6GHz i7, 8GB RAM, Antiglare Screen
|
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: 2012 Non-Retina 15" MBP; 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 | |
![]() Member Since: Jun 03, 2011
Posts: 54
![]() |
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 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];
}
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 | |||||
|
Member Since: Feb 25, 2009
Posts: 2,084
![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2012 Non-retina MBP, 2.6GHz i7, 8GB RAM, Antiglare Screen
|
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: 2012 Non-Retina 15" MBP; 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 | |
|
Member Since: Feb 25, 2009
Posts: 2,084
![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2012 Non-retina MBP, 2.6GHz i7, 8GB RAM, Antiglare Screen
|
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: 2012 Non-Retina 15" MBP; 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 | |
![]() Member Since: Jun 03, 2011
Posts: 54
![]() |
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 |
| Thread Tools | |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
|||||||
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 05:38 AM.
Powered by vBulletin