gpt4 book ai didi

ios - 如何将 NSXMLParser 限制为给定元素之后的元素

转载 作者:行者123 更新时间:2023-11-28 06:40:23 26 4
gpt4 key购买 nike

这个 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)

}
}
}

最佳答案

在处理名为 layout-key 的元素中,查看其值是否等于某个值,例如k-p12h-n13-1,如果是,设置一些状态变量,例如是 InN13。然后,在处理 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
}

关于ios - 如何将 NSXMLParser 限制为给定元素之后的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com