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

  • SEARCH
  • Home
  • 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 317435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:28:32+00:00 2026-05-12T08:28:32+00:00

I need to parse an XML record that represents a QuizQuestion. The type attribute

  • 0

I need to parse an XML record that represents a QuizQuestion. The “type” attribute tells the type of question. I then need to create an appropriate subclass of QuizQuestion based on the question type. The following code works ([auto]release statements omitted for clarity):

QuizQuestion *question = [[QuizQuestion alloc] initWithXMLString:xml];
if( [ [question type] isEqualToString:@"multipleChoiceQuestion"] ) {
    [myQuestions addObject:[[MultipleChoiceQuizQuestion alloc] initWithXMLString:xml];
}

//QuizQuestion.m
-(id)initWithXMLString:(NSString*)xml {
    self.type = ...// parse "type" attribute from xml
    // parse the rest of the xml
}

//MultipleChoiceQuizQuestion.m
-(id)initWithXMLString:(NSString*)xml {
    if( self= [super initWithXMLString:xml] ) {
        // multiple-choice stuff
    }
}

Of course, this means that the XML is parsed twice: once to find out the type of QuizQuestion, and once when the appropriate QuizQuestion is initialized.

To prevent parsing the XML twice, I tried the following approach:

// MultipleChoiceQuizQuestion.m
-(id)initWithQuizRecord:(QuizQuestion*)record {
    self=record; // record has already parsed the "type" and other parameters
    // multiple-choice stuff
}

However, this fails due to the “self=record” assignment; whenever the MultipleChoiceQuizQuestion tries to call an instance-method, it tries to call the method on the QuizQuestion class instead.

Can someone tell me the correct approach for parsing XML into the appropriate subclass when the parent class needs to be initialized to know which subclass is appropriate?

  • 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-12T08:28:32+00:00Added an answer on May 12, 2026 at 8:28 am

    I suppose you are using NSXMLParser to parse the XML string, and using some of the events it fires to parse the XML source. In MultipleChoiceQuizQuestion, you could overwrite the NSXMLParser’s delegate methods, like:

    - (void)parser:(NSXMLParser *)parser
        didStartElement:(NSString *)elementName
        namespaceURI:(NSString *)namespaceURI
        qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        [super parser:parser
            didStartElement:elementName
            namespaceURI:namespaceURI
            qualifiedName:qName attributes:attributeDict]
    
        // Multiple-choice specific stuff goes here.
    }
    

    Then call self = [super initWithXMLString:xml] as suggested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • 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 Both py2exe and pyinstaller (NOTE: for the latter use the… May 12, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer $('<div></div>').load("/url/to/load.html").appendTo('body'); will create a div on the fly then make… May 12, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer Because G2.pm needs to include a use G1; line. Without… May 12, 2026 at 11:40 am

Related Questions

I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
Am writing a .net windows service using which i need to parse an xml
I need to parse an xml string and find values of specific text nodes,
I need to be able to parse an xml file inside photoshop, using javascript.
I'm trying to use XPath to parse an XML document. One of my NSXMLElement's

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.