XML Parsing Question

Discussion in 'iOS Programming' started by thabart, Mar 23, 2011.

  1. thabart macrumors newbie

    Mar 22, 2011
    Dear all,

    I always considered myself to be an OK programmer when it came to HTML, PHP and C/C++ but the iOS SDK and Xcode is really frustrating me. It feels like a complete different world.

    I'm trying to realize a freeware program that displays my college's cafeteria daily menu. For this purpose they provided me with a XML interface, with a rather complicated structure.

    Now I'm following this tutorial (click) to implement a basic XML parser. So far so good. In this example every element only appears once per node (i.e. no more than one date per blog entry). The XML I'm working with, however, looks something like this:

     <group type="1" location="106" productiondate="2011-03-22" sort="8">
        <name>Meal name</name>
        <internalname>Meal A</internalname>
        <prices size="3">
          <price consumerID="0">2.7</price> //prices depend on status (i.e. student, staff, faculty) 
          <price consumerID="1">4.2</price>
          <price consumerID="2">6.0</price>
        <taggings size="3">
          <tagging taggingID="51">Pork</tagging> //details like meat type and additives
          <tagging taggingID="1">Color-Additives</tagging>
          <tagging taggingID="2">Preservatives</tagging>
        <components size="3">
          <component elementid="127048" sort="1"> 
            <name1>Meal Description</name1>
          <component elementid="127049" sort="2">
            <name1>Side dish 1</name1>
          <component elementid="127050" sort="3">
            <name1>Side dish 2</name1>
    How can I combine all name1 elements per group node in one array? Also I don't quite understand how this line works:

    int storyIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    	[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; 
    	return cell;
    How can I include all name1, one price and all tagging elements in one row displayed in a UITable? Also I'd like to create a new UITable per group node on the same page. What is the best way to go about this?

    Any help is highly appreciated and if you could adjust the sample code to process my XML I'd love you forever ;)
  2. namanhams macrumors regular

    Jun 3, 2009
    I work with XML all the time and i always use the TouchXML library. It's easy to use. I understand your requirement and i know how to do it with TouchXML.
    The tutorial in the link doesn't use another way to parse XML. So i can't help much.
  3. thabart thread starter macrumors newbie

    Mar 22, 2011
    Thanks for your reply, namanhams. I wouldn't mind using another parser. Can you get me started?
  4. KnightWRX macrumors Pentium


    Jan 28, 2009
    Quebec, Canada
    Have you looked up NSXMLParser and NSMutableArray ?

    That's usually how I do it. The documentation is good enough that you should be able to implement something rapidly. You only need to hook it up as a protocol in your own class and implement 3 methods, though there's nothing really classy about it, I always found it to be rather brute forcish with switch statements and tons of ifs.

    (going from your XML, when you encounter the group element, you'd alloc a MealClass and init it with the type, location, blah blah) then you'd set a variable stating you're parsing a group. You'd then go through the elements, allocating prices as a mutable array/adding prices to the array, setting other components of MealClass and once you're done parsing the group, you'd just add it to your Menu MutableArray and move on to the next group).
  5. thabart thread starter macrumors newbie

    Mar 22, 2011
    KnightWRX, thanks for your reply. The more I read the more I feel this is so much over my head. Any chance you can help me out a little more? Sounded like this project and XML seem really easy for you;)

Share This Page