NSXML help required

Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
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
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
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
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
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.
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
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?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
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.
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
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.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top