0

I need to parse the xml listed below in the iphone. can anyone help me in this. please...!

Thanks in advance...

<Assignments>
<Course>
    <CourseName>Foundation of English</CourseName>
    <CourseGradePercentage>89</CourseGradePercentage>
    <AssignmentList>
        <Assignment>
            <AssignmentName>Foundations of English I</AssignmentName>
            <Week>Week 1</Week>
            <AssignmentGrade>2</AssignmentGrade>
            <AssignmentGradePercentage>20</AssignmentGradePercentage>
        </Assignment>
        <Assignment>
            <AssignmentName>Foundations of Maths I</AssignmentName>
            <Week>Week 1</Week>
            <AssignmentGrade>2</AssignmentGrade>
            <AssignmentGradePercentage>20</AssignmentGradePercentage>
        </Assignment>
    </AssignmentList>
</Course>
<Course>
    <CourseName>Foundation of Maths</CourseName>
    <CourseGradePercentage>92</CourseGradePercentage>
    <AssignmentList>
        <Assignment>
            <AssignmentName>Foundations of English I</AssignmentName>
            <Week>Week 1</Week>
            <AssignmentGrade>4</AssignmentGrade>
            <AssignmentGradePercentage>40</AssignmentGradePercentage>
        </Assignment>
        <Assignment>
            <AssignmentName>Foundations of Maths I</AssignmentName>
            <Week>Week 1</Week>
            <AssignmentGrade>4</AssignmentGrade>
            <AssignmentGradePercentage>40</AssignmentGradePercentage>
        </Assignment>
    </AssignmentList>
</Course>

What i need to get is this..

Foundation of English1 99.99%

Week1 Assignment 10 (100%) Week2 Assignment 8 (80%) Week3 Assignment 10 (100%)

Foundation of Mathematics1 100%

Week1 Assignment 10 (100%)

This is just a sample data.. this is the format which i need to display on the iphone table view.. Please Help me..

Thanks, Shibin shibin.moideen@mediaus.com

  • You need to post you xml sample as a code block - in the example shown above all xml tags got stripped! – Shirkrin Oct 01 '09 at 06:51
  • `CourseGradePercentage` should be `GradePercentage`. `AssignmentGradePercentage` should be `GradePercentage`. Notice that you can then reuse the same code for parsing. And Week 1 is also redundant. You could just have 1. Better yet would be . – Dave Jarvis Oct 01 '09 at 22:20

2 Answers2

3

Consider using libxml2 with an Objective-C front provided by this useful set of wrapper functions.

You issue an XPath query to your XML document object and get back Foundation class objects: NSArray, NSString, and NSDictionary, e.g.:

//
// Generate NSData* object called _xmlData which is the XML data
// 

// ...

//
// Query this data for course names
//

NSArray *_queriedCourseNames = PerformXMLXPathQuery(_xmlData, @"//*[local-name()='Course']/*[local-name()='CourseName']");
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • ok thanks, but i cant understand this one. also i am using the NSXMLParser rather than libxml2 –  Oct 01 '09 at 11:16
  • can you help me on this http://stackoverflow.com/questions/6058694/getting-zero-objects-from-the-xml-file-in-the-array – ajay May 19 '11 at 16:26
0

Instead of 'Course,' why do you want to use local-name?

//Course/CourseName 

should just work, right?

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30