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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:44:16+00:00 2026-06-01T08:44:16+00:00

I have a file xml as this: <data> <first> <city> city </city> <people> 400

  • 0

I have a file xml as this:

<data>
    <first>
        <city>
            city
        </city>
        <people>
            400
        </people>
    </first>
    <size>
        <width>
            340
        </width>
        <height>
            120
        </height>
    </size>
    <description>
        <temp>
            sunny
        </temp>
        <people>
            45
        </people>
    </description>
    <description>
        <temp>
            cloudy
        </temp>
        <people>
                90
        </people>
    </description>

I use for parsing this code:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            

    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"first"]) {
        firstType = [[NSMutableDictionary alloc] init];
        currentCity = [[NSMutableString alloc] init];
        currentPeople = [[NSMutableString alloc] init];
    }

    if ([elementName isEqualToString:@"size"]){
        currentSize = [[NSMutableDictionary alloc] init];
        width = [[NSMutableString alloc]init];
        height = [[NSMutableString alloc]init];
    }

    if ([elementName isEqualToString:@"description"]){
        desc1 = [[NSMutableDictionary alloc] init];
        temp1 = [[NSMutableString alloc]init];
        people1 = [[NSMutableString alloc]init];
    }

    if ([elementName isEqualToString:@"description"]){
        desc2 = [[NSMutableDictionary alloc] init];
        temp2 = [[NSMutableString alloc]init];
        people2 = [[NSMutableString alloc]init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     

    if ([elementName isEqualToString:@"first"]) {

        [firstType setObject:currentType forKey:@"city"];
        [firstType setObject:currentQuery forKey:@"people"];
                [feed addObject:[firstType copy]];
    }
    if ([elementName isEqualToString:@"size"]){

        [currentSize setObject:tempC forKey:@"width"];
        [currentSize setObject:tempF forKey:@"height"];
        [feed addObject:[currentSize copy]];
    }
    if ([elementName isEqualToString:@"description"]){

        [desc1 setObject:temp1 forKey:@"temp1"];
        [desc1 setObject:people1 forKey:@"people1"];
        [feed addObject:[desc1 copy]];
    }

    if ([elementName isEqualToString:@"description"]){

        [desc2 setObject:temp1 forKey:@"temp2"];
        [desc2 setObject:people1 forKey:@"people2"];
        [feed addObject:[desc2 copy]];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"found");
    if ([currentElement isEqualToString:@"city"]){
        [currentCity appendString:string];
    } 
    else if ([currentElement isEqualToString:@"people"]) {
        [currentPeople appendString:string];
    } 
    else if ([currentElement isEqualToString:@"width"]){
        [width appendString:string];
    }
    else if ([currentElement isEqualToString:@"height"]){
        [height appendString:string];
    }
    else if ([currentElement isEqualToString:@"temp"]){
        [temp1 appendString:string];
    }
    else if ([currentElement isEqualToString:@"temp"]){
        [temp2 appendString:string];
    }
    else if ([currentElement isEqualToString:@"people"]){
        [people1 appendString:string];
    }
    else if ([currentElement isEqualToString:@"people"]){
        [people2 appendString:string];
    }

}

- (void) parserDidEndDocument:(NSXMLParser *)parser {

    NSLog(@"feed:%@",feed);
}

the result of nslog is:

feed:(
        {
        city = city;
        people = 4004590;
    },
        {
        width = 340;
        height = 120;
    },
        {
        temp = sunny;
        people = "";
    },
        { ///???? here there is an empty space
    },
        {
        temp = cloudy;
        people = "";
    },
        {
    }
)

Now I don’t understand why there is a space between first dictionary of desc 1 and desc2, and I don’t know how “people” take the result of people1 and people2 in a single string

Can you help me?

  • 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-06-01T08:44:17+00:00Added an answer on June 1, 2026 at 8:44 am

    You need to keep track of whether you are parsing the first occurrence of description within the data tag, or the second. This can easily be done with a boolean (if there are only two), or an integer (for multiple) that indicates which of the tags you are currently processing. Then, in the parser:didEndElement: method, you can assign the accumulated data to the correct dictionary based on the flag/counter.

    The other possibility, which I use in my XML parsing, is to accumulate characters for one tag at a time, then when I encounter the closing element for that tag, store the characters into the containing element’s Dictionary right then. In other words, when I encounter the endTag for temp, I will immediately assign it to the current description tag’s dictionary. Then, when I encounter the ending tag for the description tag itself, I can close out that dictionary, set the flag/increment the counter, and continue on with the next tag that is to be parsed.

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        // ... SNIP ...
    
        if ( [ elementName isEqualToString:@"description" ] )
        {
            curDescription = [ [ NSMutableDictionary alloc ] init ] ;
        }
    
        // ... SNIP ...
    
        accumulatedCharacters = [ [ NSMutableString alloc ] init ] 
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName    
    {
        // ... SNIP ...
    
        if ( [ elementName isEqualToString:@"temp" ] )
        {
            [ curDescription setValue:accumulatedCharacters forKey:@"temp" ] ;
        }
        if ( [ elementName isEqualToString:@"description" ] )
        {
            // Save the curDescription object, then clear it for reuse on the next 
            // occurrence of the tag
            [ curDescription release ] ;
            curDescription = nil ;
        }
    
        // ... SNIP ...
    
        [ accumulatedCharacters release ] ;
        accumulatedCharacters = nil ;
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    {
        [ accumulatedCharacters appendString:string ] ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file of this Kind : <data> <First id=FirstOne> <lines id=Lines>
I have this data from a xml file: <?xml version=1.0 encoding=utf-8 ?> <words> <id>...</id>
I have this below XML file <book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first>
I have an XML file of the following structure <Root> <Child Name=First> <Data Name=a
I have this xml file: <?xml version=1.0 encoding=UTF-8?> <Favorites> <favorite id=1> <title>My first favorite</title>
I have this xml file which has text init. i.e Hi my name is
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I have this XML at http://localhost/file.xml : <?xml version=1.0 encoding=utf-8?> <val:Root xmlns:val=http://www.hw-group.com/XMLSchema/ste/values.xsd> <Agent> <Version>2.0.3</Version>
I have a XML file like this <?xml version=1.0 encoding=utf-8?> <Survey Name=Aerosmith's Survey Id=S2
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>

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.