我想只为学生“Karly”解析学生的日程安排。这里的任务意味着 - 类。所以我需要首先去学生信息,然后找到一个名叫“Karly”的学生,然后为她解析任务。 这是我的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>
我在这里也有这个代码用于NSXMLParser,它显示了所有学生的所有内容:
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)
}
}
非常感谢!!!
答案 0 :(得分:0)
为专门的搜索任务编写解析器是没有意义的。如果可能,最好解析整个 XML并使用结果。您的XML非常简单,可以轻松地解析为Swift对象。
所以我要做的就是编写解析器来读取整个XML 并创建一个student
对象数组,其中每个student
对象都有一个name
}属性,phone
属性,uniqueID
属性和tasks
属性。 tasks
属性是task
个对象的数组,其中每个task
都具有与<task>
元素的子元素对应的属性。
现在你有了一个Swift数组,所以找到student
为name
的{{1}}并查阅该学生的任务将是微不足道的。 XML将会消失;您将完全使用Swift对象,并可以继续使用数据执行任何操作。