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 将与链接 1 一起返回);大概是因为“链接”也出现在根目录中?

任何帮助,将不胜感激!

非常感谢。

4

1 回答 1

0

经过一些更多的研究,我设法让这个工作。我重新设计了 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)
    }
}
于 2016-02-07T20:31:21.383 回答