【问题标题】:how to parse xml for specific element in swift如何在swift中解析特定元素的xml
【发布时间】: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”解析任务。
  • 好吧,这不会发生,因为您没有&lt;student-info&gt; 的解析器。您的解析器仅适用于 &lt;task&gt;
  • 是的,我知道。我正在尝试改变这一点,这就是我发布问题的原因:)
  • 但是没有人会为您编写代码。而且您的问题太模糊,无法以任何其他方式回答。我可以向您建议我会使用的策略(我已经这样做了)。但这不是“免费编写我的代码”服务。

标签: swift nsxmlparser


【解决方案1】:

为专门的搜索任务编写解析器是没有意义的。如果可能,最好解析整个 XML 并处理结果。你的 XML 非常简单,可以很容易地解析成 Swift 对象。

所以我要做的是编写你的解析器来读取整个 XML 并创建一个student 对象数组,其中每个student 对象都有一个name 属性,一个@987654324 @ 属性、uniqueID 属性和 tasks 属性。 tasks 属性将是 task 对象的数组,其中每个 task 具有与 &lt;task&gt; 元素的子元素对应的属性。

现在您已经有了一个 Swift 数组,因此找到studentname"Karly" 并查阅该学生的任务将是微不足道的。 XML 将消失;您将完全使用 Swift 对象,并且可以继续对数据做任何您想做的事情。

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多