【发布时间】:2016-02-16 18:35:35
【问题描述】:
我只想为学生“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)
}
}
非常感谢!!!
【问题讨论】:
-
有什么问题?
-
我只想为学生“Karly”解析任务。
-
好吧,这不会发生,因为您没有
<student-info>的解析器。您的解析器仅适用于<task>。 -
是的,我知道。我正在尝试改变这一点,这就是我发布问题的原因:)
-
但是没有人会为您编写代码。而且您的问题太模糊,无法以任何其他方式回答。我可以向您建议我会使用的策略(我已经这样做了)。但这不是“免费编写我的代码”服务。
标签: swift nsxmlparser