2

NSXMLParser を使用して XML RSS フィードを解析し、配列ディクショナリを作成してテーブル ビューに表示しようとしています - これは機能していますが、一致しない結果が得られます。各結果について、各「アイテム」(つまり各子) の「タイトル」要素と「ソース」要素を表示しようとしていますが、親ノードに「タイトル」が存在するため、結果の不一致が生じています。

item以下の要素のみパースするように指定することはできますか? ["items"]["title"] のようなものですか? 解析している構造のサンプルを次に示します。

<rss xmlns:atom="example" xmlns:dc="http://example" version="2.0">
<channel>
<title>Main title</title>
<link>http://main-site.com/xmltest</link>
<description>Main description</description>
<pubDate>Sun, 07 Feb 2016 10:41:05 +0000</pubDate>
    <item>
        <title>Title 1</title>
        <link>https://item1.com</link>
        <description>Description1</description>
        <pubDate>Date1</pubDate>
        <source>Source1</source>
    </item>
    <item>
        <title>Title 2</title>
        <link>https://item2.com</link>
        <description>Description2</description>
        <pubDate>Date2</pubDate>
        <source>Source2</source>
    </item>
    <item>
        <title>Title 3</title>
        <link>https://item3.com</link>
        <description>Description3</description>
        <pubDate>Date3</pubDate>
        <source>Source3</source>
    </item>
</channel>

私が持っている NSXML パーサー コードは次のとおりです。

func parser(parser: NSXMLParser,
    didStartElement elementName: String,
    namespaceURI: String?,
    qualifiedName: String?,
    attributes attributeDict: [String : String]){
        if elementName == "title"{
            entryTitle = String()
            currentParsedElement = "title"
        }
        if elementName == "description"{
            entryDescription = String()
            currentParsedElement = "description"
        }
        if elementName == "link"{
            entryLink = String()
            currentParsedElement = "link"
        }
        if elementName == "source"{
            entrySource = String()
            currentParsedElement = "source"
        }
        if elementName == "pubDate"{
            entryDate = String()
            currentParsedElement = "pubDate"
        }
}

func parser(parser: NSXMLParser,
    foundCharacters string: String){
        if currentParsedElement == "title"{
            self.entryTitle = entryTitle + string
        }
        if currentParsedElement == "description"{
            self.entryDescription = entryDescription + string
        }
        if currentParsedElement == "link"{
            self.entryLink = entryLink + string
        }
        if currentParsedElement == "source"{
            self.entrySource = entrySource + string
        }
        if currentParsedElement == "pubDate"{
            self.entryDate = entryDate + string
        }

}

func parser(parser: NSXMLParser,
    didEndElement elementName: String,
    namespaceURI: String?,
    qualifiedName qName: String?){
        if elementName == "title"{
            entryDictionary["title"] = entryTitle
        }
        if elementName == "link"{
            entryDictionary["link"] = entryLink
        }
        if elementName == "description"{
            entryDictionary["description"] = entryDescription
        }
        if elementName == "source"{
            entryDictionary["source"] = entrySource
        }
        if elementName == "pubDate"{
            entryDictionary["pubDate"] = entryDate
            entriesArray.append(entryDictionary)
        }

}

func parserDidEndDocument(parser: NSXMLParser){
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
}

これから得られる結果は次のとおりです。

  1. メインタイトル(出典なし)
  2. タイトル1、(出典なし)
  3. タイトル 2、ソース 1
  4. タイトル 3、ソース 2

これは、ルート要素と子要素に「タイトル」が存在するのに対し、ソースには存在しないために発生していますか? もしそうなら、どうすれば最初のルート要素を除外できますか?

他の値 (リンクなど) も取得していますが、これらの値は一致します (つまり、タイトル 1 が Link1 で返されます)。おそらく「リンク」もルートに表示されるためでしょうか?

どんな助けでも大歓迎です!

どうもありがとう。

4

1 に答える 1