我正在尝试使用 NSXMLParser 解析 XML RSS 提要并创建一个数组字典并显示在表视图中 - 这正在工作,但是我得到的结果不匹配。对于每个结果,我试图显示每个"项目"(即每个子项(的"标题"和"源"元素 - 但由于父节点中存在"标题",因此会产生不匹配的结果。
是否可以指定仅解析 item 下的元素? 即类似 ["项目"]["标题"]?这是我正在解析的结构示例:
<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 将与 Link1 一起返回(;大概是因为"链接"也出现在根中?
任何帮助将不胜感激!
非常感谢。
经过更多的研究,我设法让它工作。我重新设计了 XML 解析器,以便我明确指定从"item"节点开始解析;有效地排除根/父节点。重构的解析器如下所示:
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
source = NSMutableString()
source = ""
pubDate = NSMutableString()
pubDate = ""
link = NSMutableString()
link = ""
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !source.isEqual(nil) {
elements.setObject(source, forKey: "source")
}
if !pubDate.isEqual(nil) {
elements.setObject(pubDate, forKey: "pubDate")
}
if !link.isEqual(nil) {
elements.setObject(link, forKey: "link")
}
posts.addObject(elements)
}
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("title") {
title1.appendString(string)
} else if element.isEqualToString("source") {
source.appendString(string)
}
else if element.isEqualToString("pubDate") {
pubDate.appendString(string)
}
else if element.isEqualToString("link"){
link.appendString(string)
}
}