作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个 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/
我是一名优秀的程序员,十分优秀!