Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 521783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:17:09+00:00 2026-05-13T08:17:09+00:00

Does anyone have some idea how to parse the following xml using event-driven model

  • 0

Does anyone have some idea how to parse the following xml using event-driven model NSXMLParser class?

<Node>
  <name> Main </name>
  <Node>
    <name> Child 1 </name>
  </Node>

  <Node>
    <name> Child 2 </name>
  </Node>
</Node>

I want to collect all three names from this xml file, is it possible, or I have to change to Tree-based parsing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:17:09+00:00Added an answer on May 13, 2026 at 8:17 am

    This is a common problem with parsers like this one, of “type SAX”, where you have to manually keep track of the current depth of the XML tree you’re in. The problem, as always, is that loading the entire tree in a DOM structure in memory can be impossible, depending on the size of the data you want to manipulate.

    The following code shows a class that does this job:

    #import <Foundation/Foundation.h>
    
    @interface Test : NSObject <NSXMLParserDelegate> 
    {
    @private
        NSXMLParser *xmlParser;
        NSInteger depth;
        NSMutableString *currentName;
        NSString *currentElement;
    }
    
    - (void)start;
    
    @end
    

    This is the implementation:

    #import "Test.h"
    
    @interface Test ()
    - (void)showCurrentDepth;
    @end
    
    
    @implementation Test
    
    - (void)dealloc
    {
        [currentElement release];
        [currentName release];
        [xmlParser release];
        [super dealloc];
    }
    
    - (void)start
    {
        NSString *xml = @"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Node><name>Main</name><Node><name>Child 1</name></Node><Node><name>Child 2</name></Node></Node>";
        xmlParser = [[NSXMLParser alloc] initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
        [xmlParser setDelegate:self];
        [xmlParser setShouldProcessNamespaces:NO];
        [xmlParser setShouldReportNamespacePrefixes:NO];
        [xmlParser setShouldResolveExternalEntities:NO];
        [xmlParser parse];
    
    }
    
    #pragma mark -
    #pragma mark NSXMLParserDelegate methods
    
    - (void)parserDidStartDocument:(NSXMLParser *)parser 
    {
        NSLog(@"Document started", nil);
        depth = 0;
        currentElement = nil;
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
    {
        NSLog(@"Error: %@", [parseError localizedDescription]);
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI 
     qualifiedName:(NSString *)qName 
        attributes:(NSDictionary *)attributeDict
    {
        [currentElement release];
        currentElement = [elementName copy];
    
        if ([currentElement isEqualToString:@"Node"])
        {
            ++depth;
            [self showCurrentDepth];
        }
        else if ([currentElement isEqualToString:@"name"])
        {
            [currentName release];
            currentName = [[NSMutableString alloc] init];
        }
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI 
     qualifiedName:(NSString *)qName
    {
    
        if ([elementName isEqualToString:@"Node"]) 
        {
            --depth;
            [self showCurrentDepth];
        }
        else if ([elementName isEqualToString:@"name"])
        {
            if (depth == 1)
            {
                NSLog(@"Outer name tag: %@", currentName);
            }
            else 
            {
                NSLog(@"Inner name tag: %@", currentName);
            }
        }
    }        
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        if ([currentElement isEqualToString:@"name"]) 
        {
            [currentName appendString:string];
        } 
    }
    
    - (void)parserDidEndDocument:(NSXMLParser *)parser 
    {
        NSLog(@"Document finished", nil);
    }
    
    #pragma mark -
    #pragma mark Private methods
    
    - (void)showCurrentDepth
    {
        NSLog(@"Current depth: %d", depth);
    }
    
    @end
    

    This is the result of running a command line tool that triggers the “start” method above:

    Document started
    Current depth: 1
    Outer name tag: Main
    Current depth: 2
    Inner name tag: Child 1
    Current depth: 1
    Current depth: 2
    Inner name tag: Child 2
    Current depth: 1
    Current depth: 0
    Document finished
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 269k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well I think you are misusing the modal view controller.… May 13, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer Why not simply use W3C GeoLocation API available in mobile… May 13, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer I assume you want HTML as the output. You need… May 13, 2026 at 1:24 pm

Related Questions

I would like to add an overlay image on a Google Map. The image
I'm running the following code; using (WebClient wc = new WebClient()) { string page
In my application, I alter some part of XML files, which begin like this:
After reading some stuff it seems I can map the SMBIOS memory and parse
I'm writing a help desk pipe handler to pipe incoming e-mails as helpdesk ticket

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.