gpt4 book ai didi

arrays - 使用 swift 解析 xml 数组并将数据存储到数组或字典中

转载 作者:行者123 更新时间:2023-11-30 10:13:21 27 4
gpt4 key购买 nike

我需要使用 nsxmlparser 解析 xml 数据。但是我在获取所需数据并使用 swift 将其存储到数组或字典时遇到问题。我已经实现了委托(delegate)函数,并且能够找到这样的元素名称

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject])
{
println(elementName)
if(elementName == "subCategories")
{
categoryName: String = [attributeDict valueForKey:"category"];
id = [attributeDict valueForKey:"id"];
name = [attributeDict valueForKey:"name"];
// Now You can store these in your preferable data models - data objects/array/dictionary
}

但是我无法使用 swift 实现相同的功能。我的 xml 如下所示

    <category>
<id>cat00000</id>
<name>Best Buy</name>
<active>true</active>
<path>
<category>
<id>cat00000</id>
<name>Best Buy</name>
</category>
</path>
<subCategories>
<category>
<id>abcat0100000</id>
<name>TV &amp; Home Theater</name>
</category>
<category>
<id>abcat0200000</id>
<name>Audio</name>
</category>
<category>
<id>abcat0300000</id>
<name>Car Electronics &amp; GPS</name>
</category>

最佳答案

您发布的 Swift 和 Objective-C 的奇怪组合,看起来您正在尝试读取每个 <category> 的属性,但实际上你需要获取 <category> 每个子节点的值。试试这个:

class CategoryParser: NSObject, NSXMLParserDelegate {

var subcategories = [[String : String]]()
var currentSubcategory: [String : String]?
var currentElementName: String?

var completion: (([[String : String]]) -> ())?

func parseXML( string: String, completion: (([[String : String]]) -> ())? ) {
guard let data = string.dataUsingEncoding( NSUTF8StringEncoding ) else {
fatalError( "Base XML data" )
}
self.completion = completion
let parser = NSXMLParser(data: data )
parser.delegate = self
parser.parse()
}

func parserDidEndDocument(parser: NSXMLParser) {
self.completion?( self.subcategories )
}

func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
print( "Parse error: \(parseError)" )
}

func parser(parser: NSXMLParser, foundCharacters string: String) {

if let elementName = self.currentElementName {
if [ "id", "name" ].contains( elementName ) {
self.currentSubcategory?[ elementName ] = string
}
}
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "category", let subcategory = self.currentSubcategory {
self.subcategories.append( subcategory )
self.currentSubcategory = nil
}
}

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

if elementName == "category" {
self.currentSubcategory = [String : String]()
}
else {
self.currentElementName = elementName
}
}
}



let categoryParser = CategoryParser()
let xmlString = "<subCategories><category><id>someId</id><name>someName</name></category></subCategories>"
categoryParser.parseXML( xmlString ) { (categories) in
print( categories )
}

关于arrays - 使用 swift 解析 xml 数组并将数据存储到数组或字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694580/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com