-1

I would like to parse student schedule only for the student "Karly". Task here means - classes. So I need to go to student-info first, and find out a student whose name is "Karly" and then parse task for her. Here's my XML:

<?xml version="1.0"?>
<students-scheule-information>
    <student-schedule>
        <student-info>
            <name>Jiyoon Lee</name>
            <phone>765-637-8532</phone>
            <uniqueID>S201501</uniqueID>
        </student-info>
        <week-schedule>
            <task>
                <notificationID>notification1_1</notificationID>
                <date>20151112</date>
                <starttime>1200</starttime>
                <endtime>1305</endtime>
                <location>knoy376</location>
                <title>Math</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_2</notificationID>
                <date>20151111</date>
                <starttime>1200</starttime>
                <endtime>1305</endtime>
                <location>knoy376</location>
                <title>History</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_3</notificationID>
                <date>20151113</date>
                <starttime>0930</starttime>
                <endtime>1200</endtime>
                <location>knoy376</location>
                <title>Geography</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_4</notificationID>
                <date>20151109</date>
                <starttime>1200</starttime>
                <endtime>1735</endtime>
                <location>knoy376</location>
                <title>Art</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_5</notificationID>
                <date>20151109</date>
                <starttime>2000</starttime>
                <endtime>2100</endtime>
                <location>knoy376</location>
                <title>Intermediate German</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
        </week-schedule>
    </student-schedule>
    <student-schedule>
        <student-info>
            <name>Karly</name>
            <phone>765-637-8222</phone>
            <uniqueID>S201502</uniqueID>
        </student-info>
        <week-schedule>
            <task>
                <notificationID>notification3</notificationID>
                <date>20151109</date>
                <starttime>2210</starttime>
                <endtime>2305</endtime>
                <location>knoy376</location>
                <title>Chemisrty</title>
                <comments>Cool</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_5</notificationID>
                <date>20151109</date>
                <starttime>2000</starttime>
                <endtime>2100</endtime>
                <location>knoy376</location>
                <title>History</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_5</notificationID>
                <date>20151112</date>
                <starttime>2000</starttime>
                <endtime>2100</endtime>
                <location>knoy376</location>
                <title>Math</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_5</notificationID>
                <date>20151111</date>
                <starttime>1130</starttime>
                <endtime>1420</endtime>
                <location>knoy376</location>
                <title>Biology</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
            <task>
                <notificationID>notification1_5</notificationID>
                <date>20151113</date>
                <starttime>1000</starttime>
                <endtime>1300</endtime>
                <location>knoy376</location>
                <title>Geography</title>
                <comments>good</comments>
                <image>X</image>
                <ischeckable>true</ischeckable>
                <repeatnum>3</repeatnum>
                <repeatinterval>5</repeatinterval>
            </task>
        </week-schedule>
    </student-schedule>
</students-scheule-information>

I also have this code here for NSXMLParser which shows all for both students:

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

    anElement = elementName
        if elementName == "task" {
                title = String()
            }

}

func parser(parser: NSXMLParser, foundCharacters string: String) {
    let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
  if (!data.isEmpty) {
    switch anElement {
        case "title":
        aTitle = data
        case "starttime":
        aStartTime = data
        case "endtime":
        anEndTime = data
        case "location":
        aLocation = data
    default:
        print("Check your file")
    }


   }
}


func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if elementName == "task" {
        let oneClass: TheClass = TheClass()
        oneClass.title = aTitle
        oneClass.location = aLocation
        oneClass.startTime = aStartTime
        oneClass.endTime = anEndTime
        oneClass.classColor = aClassColor
        classList.append(oneClass)

    }
}

Thanks a lot!!!

1 Answers1

0

It doesn't make sense to write a parser for a specialized search task. It's better, if possible, to parse the whole XML and work with the results. Your XML is extremely simple, and can easily be parsed into Swift objects.

So what I would do is write your parser to read the entire XML and create an array of student objects, where each student object has a name property, a phone property, a uniqueID property, and a tasks property. The tasks property would be an array of task objects, where each task has properties corresponding to the subelements of the <task> element.

Now you've got a Swift array, so it will be trivial to find the student whose name is "Karly" and consult that student's tasks. The XML will be gone; you'll be working entirely with Swift objects, and can proceed to do anything you want with the data.

matt
  • 515,959
  • 87
  • 875
  • 1,141