2016-06-30 21 views
1

このXMLが下に形成される方法は、コードがn13期間とn7期間の両方を取得しているため、結果をn13データのみに限定することができますどういうわけか?NSXMLParserを特定の要素の後の要素に制限する方法

<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コード:layout-keyという名前の処理要素で

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) 

       } 
      } 
     } 
+0

どのようにノードをループしますか? –

+0

関連していませんが、 'if(elementName as NSString).isEqualToString(" foo "){...}'パターンは 'if elementName ==" foo "{...}'に単純化できます。 – Rob

答えて

1

、その値が何かに等しかったかどうかを確認し、例えばを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 
} 
関連する問題