NSXML help required

Discussion in 'iOS Programming' started by sagarshivam, Aug 22, 2011.

  1. sagarshivam macrumors member

    Joined:
    May 24, 2011
    #1
    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
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Google, NSXMLParser.. It's not that hard actually. When you have more specific questions, you could come back and ask questions. (there are 1 gazillion tut's on XML parsing, including how to parse them to objects etc.)
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    I learned how to use the NSXMLParser by looking at Apple's GKTank example app, specifically at the level loading methods.
     
  4. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #4
    Thanks to all 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
     

Share This Page