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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:32:44+00:00 2026-05-26T21:32:44+00:00

Given this XML structure from (http://localhost:3000/route.xml): <objects type=array> <object> <trip0> <departure>20:01</departure> <start>Place a</start> <end>Place

  • 0

Given this XML structure from (http://localhost:3000/route.xml):

<objects type="array">
  <object>
    <trip0>
     <departure>20:01</departure>
     <start>Place a</start>
     <end>Place b </end>
     <arrival>20:16</arrival>
     <how>Walking</how>
    </trip0>
  </object>
  <object>
    <trip1>
      <departure>20:16</departure>
      <start>Place b</start>
      <end>Place c </end>
      <arrival>20:32</arrival>
      <how>By car</how>
     </trip1>
  </object>
</objects> 

Using the following Objective-C code:

implementation of view

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // URL localhost for testing purposes
    NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000/route.xml"];

    // Init parser
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlParser setDelegate:self];   

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Errors !!!!!!!!E!!!");
}


      #pragma mark XMLParser delegate
    - (void)parserDidStartDocument:(NSXMLParser *)parser 
    {
        NSLog(@"Document started", nil);
       }


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

        if([elementName isEqualToString:@"trip0"]) {
            //Initialize the array.
            NSLog(@"Found trip0 !");
        }

        NSLog(@"Processing Element: %@", elementName);
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

        NSLog(@"Processing Value: %@", string);

    }

I’m able to get this as output :

2011-11-16 19:34:06.006 SandboxTabBar[11754:11603] Processing Element: objects
2011-11-16 19:34:06.008 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.009 SandboxTabBar[11754:11603] Processing Element: object
2011-11-16 19:34:06.009 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.010 SandboxTabBar[11754:11603] Found trip0 !!
2011-11-16 19:34:06.010 SandboxTabBar[11754:11603] Processing Element: trip0
2011-11-16 19:34:06.011 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.011 SandboxTabBar[11754:11603] Processing Element: departure
2011-11-16 19:34:06.012 SandboxTabBar[11754:11603] Processing Value: 20:01
2011-11-16 19:34:06.012 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.013 SandboxTabBar[11754:11603] Processing Element: start
2011-11-16 19:34:06.013 SandboxTabBar[11754:11603] Processing Value: Place a
2011-11-16 19:34:06.014 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.015 SandboxTabBar[11754:11603] Processing Element: end
2011-11-16 19:34:06.015 SandboxTabBar[11754:11603] Processing Value: Place b
2011-11-16 19:34:06.207 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.208 SandboxTabBar[11754:11603] Processing Element: arrival
2011-11-16 19:34:06.209 SandboxTabBar[11754:11603] Processing Value: 20:16
2011-11-16 19:34:06.210 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.211 SandboxTabBar[11754:11603] Processing Element: how
2011-11-16 19:34:06.212 SandboxTabBar[11754:11603] Processing Value: walking
2011-11-16 19:34:06.213 SandboxTabBar[11754:11603] Processing Value:  15 min 
2011-11-16 19:34:06.214 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.215 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.216 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.217 SandboxTabBar[11754:11603] Processing Element: object
2011-11-16 19:34:06.218 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.219 SandboxTabBar[11754:11603] Processing Element: trip1
2011-11-16 19:34:06.220 SandboxTabBar[11754:11603] Processing Value: 

2011-11-16 19:34:06.221 SandboxTabBar[11754:11603] Processing Element: departure
2011-11-16 19:34:06.222 SandboxTabBar[11754:11603] Processing Value: 20:32
2011-11-16 19:34:06.223 SandboxTabBar[11754:11603] Processing Value:

2011-11-16 19:34:06.221 SandboxTabBar[11754:11603] Processing Element: start
2011-11-16 19:34:06.222 SandboxTabBar[11754:11603] Processing Value: Place b
2011-11-16 19:34:06.223 SandboxTabBar[11754:11603] Processing Value:

And so on …

I want to populate UITableviews with content like this:

“Trip 0’s depature is at 20:01 from Place A to Place b and arrived on 20:16 by walking.”

and

“Trip 1’s departure is at 20:16 from Place b to Place c and arrived on 20:32 by car”

There must be an easier way to select and combine the nodes that I need from the XML file.

How to achieve such a result with the given XML file?

  • 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-26T21:32:45+00:00Added an answer on May 26, 2026 at 9:32 pm

    A DOM parser makes things much easier for things like this, but it does mean (a) choosing a library that supports DOM on iOS, figuring out how it works, its idiosyncrasies, etc and (b) having the whole XML document in memory before you parse it out and populate your table model. Neither of those is a big deal for many cases, but sometimes memory concerns may force you away from DOM.

    If you want to avoid these potential problems for your app, it isn’t that hard to implement what you want using NSXMLParser. To start, define a class that represents a “Trip” and give that class the properties you want for each trip (start, end, arrival, etc). Then in your parser delegate class define a few ivars for holding a mutable array of Trip objects, a “currentTrip” object that you can populate as you go, and a mutable character buffer you can use to capture characters scanned by the parser.

    With those in place you can implement something like the following in your parser delegate logic:

    - (void)parserDidStartDocument:(NSXMLParser *)parser 
    {
        NSLog(@"Document started", nil);
        allTrips = [[NSMutableArray alloc] init];
       }
    
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict {
    
        if([elementName hasPrefix:@"trip"]) {
            NSLog(@"Found trip: %@ !", elementName);
            currentTrip = [[Trip alloc] initWithName:elementName];
        }
        else {
            characterBuffer = [[NSMutableString alloc] init];
        }
    
        NSLog(@"Processing Element: %@", elementName);
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    
        NSLog(@"Processing Value: %@", string);
        [characterBuffer appendString:string];
    }
    
    - (void)parser:(NSXMLParser *)parser 
     didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI 
     qualifiedName:(NSString *)qName {
    
        if([elementName hasPrefix:@"trip"]) {
            [allTrips addObject:currentTrip];
        }
        else if ([elementName isEqualToString:@"start"){
            // Set start value
            NSString *value = [characterBuffer stringByTrimmingCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]];
            [currentTrip setStart:value];
        }
        // Handle each other trip property you care about with separate else if's
    
    }
    

    This is just the basic code structure you want, not necessarily optimized. In particular, I haven’t included any releases to avoid memory leaks, which you would need if you aren’t using ARC. Also, I’ve created a new characterBuffer object at the beginning of each element that could include text you care about: This might be done more intelligently with a little more care and thinking. You get the idea though.

    Taken as a whole, this approach can be a little tedious but it also is not hard and it can perform quite a bit better than DOM parsing in some cases, if that’s something that is a factor for you app.

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

Sidebar

Related Questions

Given an XML structure like this: <garage> <car>Firebird</car> <car>Altima</car> <car>Prius</car> </garage> I want to
Given an xml structure like this <gesmes:Envelope> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</gesmes:name> </gesmes:Sender>
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
how to make a good MySQL Database Table Structure from this XML-Code? <xml> <product>
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
given this xml: <root> <list> <!-- foo's comment --> <item name=foo /> <item name=bar
Given this example XML file: <doc> <tag> Hello ! </tag> <tag> My name is
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I

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.