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 6709103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:25+00:00 2026-05-26T07:50:25+00:00

I am new to programming and this is my first project so I don’t

  • 0

I am new to programming and this is my first project so I don’t have much idea about xml parsing etc.
I need to parse an xml document like…

<TLMS><TLMSResponse status="SUCCESS">
 <books>
  <book>
   <name>abc</name>
   <author>DEF</author>
   <publisher>HIJ</publisher>
  </book>

  <book>
   <name>xyz</name>
   <author>rus</author>
   <publisher>tuv</publisher>
  </book>
 </books>
</TLMSResponse></TLMS>

I have tried a lot but haven’t found any good de-serializer that can parse this xml and make objects of “Book” class.
Any help is greatly appreciated…

Thank You all

  • 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-26T07:50:26+00:00Added an answer on May 26, 2026 at 7:50 am

    Here’s an example class implementing the NSXMLParserDelegate protocol.

    It would parse this xml:

    <TLMS><TLMSResponse status="SUCCESS">
    <books>
    <book>
    <name>abc</name>
    <author>DEF</author>
    <publisher>HIJ</publisher>
    </book>
    
    <book>
    <name>xyz</name>
    <author>rus</author>
    <publisher>tuv</publisher>
    </book>
    </books>
    </TLMSResponse></TLMS>
    

    into an array of dictionaries, each dictionary is a “book” from the xml with keys for each tag and the text between that tag as the value for the key. So this is how the returnData in [_delegate acceptParsedData:[returnData autorelease] withIdent:ident]; would look like:

    array = 
    (
        {
            name:"abc",
            author:"DEF",
            publisher:"HIJ",
        },
        {
            name:"xyz",
            author:"rus",
            publisher:"tuv",
        },
    )
    

    JHXMLParser.h

    @protocol JHXMLParserDelegate;
    
    @interface JHXMLParser : NSObject <NSXMLParserDelegate> {
        NSString *ident;
    
        @private
        id _delegate;
        NSMutableArray *_parsedData;
        NSString *_key;
        NSXMLParser *_dataParser;
        NSString *_previousTag;
        NSString *_currentTag;
        NSMutableString *_elementText;
    }
    
    @property (nonatomic, retain) NSString *ident;
    
    - (id)initWithKey:(NSString *)Key andData:(NSData *)data;
    // delegate management. The delegate is not retained.
    - (id <JHXMLParserDelegate>)delegate;
    - (void)setDelegate:(id <JHXMLParserDelegate>)delegate;
    - (BOOL)start;
    
    @end
    
    @protocol JHXMLParserDelegate <NSObject>
    
    @optional
    - (void)acceptParsedData:(NSMutableArray *)parsedData withIdent:(NSString *)ident;
    
    @end
    

    JHXMLParser.m

    #import "JHXMLParser.h"
    
    @implementation JHXMLParser
    
    @synthesize ident;
    
    - (id)init {
        if ((self = [super init])) {
            ident = [[NSString alloc] init];
        }
        return self;
    }
    
    - (id)initWithKey:(NSString *)key andData:(NSData *)data {
        if ((self = [self init])) {
            _key = key;
            _elementText = [[NSMutableString alloc] initWithString:@""];
            _dataParser = [[NSXMLParser alloc] initWithData:data];
            _dataParser.delegate = self;
        }
        return self;
    }
    
    - (id <JHXMLParserDelegate>)delegate {
        id <JHXMLParserDelegate> d = nil;
        if (_delegate) {
            d = _delegate;
        }
        return d;
    }
    - (void)setDelegate:(id <JHXMLParserDelegate>)delegate {
        _delegate = delegate;
    }
    
    - (BOOL)start {
        return [_dataParser parse];
    }
    
    - (void)dealloc {
        [ident release];
        [_dataParser release];
        [_parsedData release];
        [_elementText release];
        [_previousTag release];
        [_currentTag release];
        [super dealloc];
    }
    
    #pragma mark - NSXMLParser Delegate
    
    - (void)parserDidStartDocument:(NSXMLParser *)parser {
        _parsedData = [[NSMutableArray alloc] init];
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
        if (_currentTag) {
            [_currentTag release], _currentTag = nil;
        }
        _currentTag = [[NSString alloc] initWithString:elementName];
        if ([elementName isEqualToString:_key]) {
            NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
            [_parsedData addObject:tmpDict];
            [tmpDict release];
        }
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        if ([_previousTag isEqualToString:_currentTag]) {
            [_elementText appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        } else {
            if (_elementText) {
                [_elementText release], _elementText = nil;
            }
            _elementText = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        }
        if (![_previousTag isEqualToString:_currentTag]) {
            if (_previousTag) {
                [_previousTag release], _previousTag = nil;
            }
            _previousTag = [[NSString alloc] initWithString:_currentTag];
        }
        [pool drain];
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        if ([_previousTag isEqualToString:elementName]) {
            [[_parsedData lastObject] setObject:_elementText forKey:elementName];
        } else {
            if (_previousTag) {
                [_previousTag release], _previousTag = nil;
            }
            _previousTag = [[NSString alloc] initWithFormat:@""];
        }
        [pool drain];
    }
    
    - (void)parserDidEndDocument:(NSXMLParser *)parser {
        NSMutableArray *returnData = [[NSMutableArray alloc] initWithArray:_parsedData];
        if ([_delegate respondsToSelector:@selector(acceptParsedData:withIdent:)]) {
            [_delegate acceptParsedData:[returnData autorelease] withIdent:ident];
        } else {
            [returnData release], returnData = nil;
        }
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
        NSLog(@"********** ERROR Parsing (%@) XML Data! - %@ - %@ **********", ident, [parseError localizedDescription], [parseError localizedFailureReason]);
        if ([_delegate respondsToSelector:@selector(acceptParsedData:withIdent:)]) {
            [_delegate acceptParsedData:_parsedData withIdent:ident];
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need your help for this. I am relatively new to programming and
This question might be naive as I'm new to ColdFusion programming. I have a
I'm new to Java, but not new to programming, so as my first project
I'm new to Cocoa programming, and decided for my first project to create a
I am new to programming for the iPhone and this will be my first
I'm very new to C# (this is my first C# project). I'm fairly confident
I don't have much experience with programming. I am working (to learn) on a
I've came across this new acronym, SOFEA, apparently a new programming paradigm for web
I am fairly new to programming and while doing a lot of reading this
i am new in programming under linux and trying to get working this code:

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.