如何在给定元素之后将NSXMLParser限制为元素

时间:2016-06-30 17:03:48

标签: ios swift nsxmlparser

下面形成这个XML的方式,我的代码同时获得n13个时间段以及n7个时间段,是否有可能以某种方式将我的结果限制为n13数据?

<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p12h-n13-1</layout-key>
<start-valid-time period-name="This Afternoon">2016-06-30T12:00:00-04:00</start-valid-time>
<start-valid-time period-name="Tonight">2016-06-30T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Friday">2016-07-01T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Friday Night">2016-07-01T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Saturday">2016-07-02T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Saturday Night">2016-07-02T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Sunday">2016-07-03T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Sunday Night">2016-07-03T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Independence Day">2016-07-04T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Monday Night">2016-07-04T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Tuesday">2016-07-05T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Tuesday Night">2016-07-05T18:00:00-04:00</start-valid-time>
<start-valid-time period-name="Wednesday">2016-07-06T06:00:00-04:00</start-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="12hourly">
<layout-key>k-p24h-n7-1</layout-key>
<start-valid-time period-name="This Afternoon">2016-06-30T12:00:00-04:00</start-valid-time>
<start-valid-time period-name="Friday">2016-07-01T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Saturday">2016-07-02T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Sunday">2016-07-03T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Independence Day">2016-07-04T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Tuesday">2016-07-05T06:00:00-04:00</start-valid-time>
<start-valid-time period-name="Wednesday">2016-07-06T06:00:00-04:00</start-valid-time>
</time-layout>

我的DidStartElement代码:

 if weAreInsideAnItem == true {

            if  (elementName as NSString).isEqualToString("start-valid-time") {

                if let periodAsNSString: NSString = attributeDict["period-name"] {
                    guard let periodAsString: String = periodAsNSString as String else {return}
                    tempForecastTimes.append(periodAsString)

                }
            }
        }

1 个答案:

答案 0 :(得分:1)

在名为layout-key的处理元素中,查看其值是否等于某事,例如k-p12h-n13-1,如果是,请设置一些状态变量,例如isInN13。然后,在处理start-valid-time时,您可以考虑该状态变量。例如:

var value: String?
var isInN13 = false

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    if elementName == "layout-key" {
        value = ""
    }

    if elementName == "start-valid-time" && isInN13 {
        value = ""
    }
}

func parser(parser: NSXMLParser, foundCharacters string: String) {
    value? += string
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if elementName == "layout-key" {
        isInN13 = value == "k-p12h-n13-1"
    }

    if elementName == "start-valid-time" && isInN13 {
        print(value)
    }

    value = nil
}