Results 1 to 7 of 7
  1. #1
    NSXML help required

    Member Since
    Jun 03, 2011
    Posts
    54
    NSXML help required
    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. #2
    NSXML help required

    Member Since
    Feb 25, 2009
    Posts
    2,109
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    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)

  3. #3
    NSXML help required

    Member Since
    Jun 03, 2011
    Posts
    54
    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

  4. #4
    NSXML help required

    Member Since
    Feb 25, 2009
    Posts
    2,109
    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.
    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)

  5. #5
    NSXML help required

    Member Since
    Jun 03, 2011
    Posts
    54
    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?

  6. #6
    NSXML help required

    Member Since
    Feb 25, 2009
    Posts
    2,109
    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.
    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)

  7. #7
    NSXML help required

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help required....?
    By Jonnykayuk in forum OS X - Operating System
    Replies: 3
    Last Post: 02-25-2015, 05:32 PM
  2. Help required
    By chistianfield in forum OS X - Operating System
    Replies: 5
    Last Post: 03-13-2010, 10:21 AM
  3. which discs are required?
    By imzd in forum OS X - Operating System
    Replies: 4
    Last Post: 12-03-2009, 03:29 PM
  4. Help required ...
    By Vulpine in forum Schweb's Lounge
    Replies: 2
    Last Post: 10-03-2006, 03:16 PM
  5. OS X Help required
    By kerplunk in forum OS X - Operating System
    Replies: 2
    Last Post: 05-13-2005, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •