gpt4 book ai didi

ios - 使用 NSXMLParser 在 Swift 中解析分层 XML

转载 作者:可可西里 更新时间:2023-11-01 02:25:43 32 4
gpt4 key购买 nike

我真的很难以我可以实际使用的形式恢复分层 XML 值,因此非常感谢任何帮助。我是 Swift 和 IOS 开发的新手,所以老实说,我并不完全了解解析器,但我希望在这之后我能理解!

这是我尝试解析的示例 XML,它来自 soap 网络服务。在连接和获取数据方面的所有方面都工作得很好

<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<get_Entry_DetailsResponse
xmlns="http://tempuri.org/">
<get_Entry_DetailsResult>
<ApiResult
xmlns="">
<Status>Success</Status>
<Parameters>
<TotalRecords>1</TotalRecords>
<Records>
<Entry>
<Entry_ID>1234</Entry_ID>
<Title>This is the Title</Title>
<Description>This is a description.</Description>
<Charge_USD>3</Charge_USD>
<Charge_GBP>1.5</Charge_GBP>
<Classifications>
<Classification_Type>No. Of Colours</Classification_Type>
<Description>10</Description>
<Classification_Type>Height</Classification_Type>
<Description>16.712</Description>
<Classification_Type>Width</Classification_Type>
<Description>1.485</Description>
<Classification_Type>Width Count</Classification_Type>
<Description>11</Description>
<Classification_Type>Pages</Classification_Type>
<Description>6</Description>
<Classification_Type>Type</Classification_Type>
<Description>Type Description</Description>
<Classification_Type>Topic</Classification_Type>
<Description>Transport</Description>
<Classification_Type>Item</Classification_Type>
<Description>Shop Item</Description>
<Classification_Type>Material</Classification_Type>
<Description>Metal</Description>
<Classification_Type>Manufacturer</Classification_Type>
<Description>Unknown</Description>
</Classifications>
</Entry>
</Records>
</Parameters>
</ApiResult>
</get_Entry_DetailsResult>
</get_Entry_DetailsResponse>
</s:Body>
</s:Envelope>

这样我就可以获得 Entry_ID、标题、描述、Charge_GBP 和 Charge_USD 等元素,这是将分类返回到代码中的方法,这样我就可以实际使用它在页面上输出它。

这是我的 connectionDidFinishLoading,我在其中启动并运行解析器:

func connectionDidFinishLoading(connection: NSURLConnection!) {
var xmlParser = NSXMLParser(data: mutableData)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
}

然后这里是 XMLParser Delegate 的东西:

var mutableData:NSMutableData  = NSMutableData.alloc()
var currentElementName:NSString = ""
var foundCharacters = ""
var appParsedData = [Dictionary<String, String>]()
var currentDataDictionary = Dictionary<String, String>()

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) {

currentElementName = elementName

}

func parser(parser: NSXMLParser, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {

if !foundCharacters.isEmpty {
currentDataDictionary[currentElementName] = foundCharacters
foundCharacters = ""

//Last Element so add to main Dictionary
//Cannot find last element yet on XML so use Charge_GBP for testing until fixed
if currentElementName == "Charge_GBP" {
appParsedData.append(currentDataDictionary)
}
}
}

func parser(parser: NSXMLParser, foundCharacters string: String!) {
if (currentElementName == "Entry_ID") || (currentElementName == "Title") || currentElementName == "Description" || currentElementName == "Charge_USD" || currentElementName == "Charge_GBP" || currentElementName == "Classification_Type" {
foundCharacters += string
}
}

func parserDidEndDocument(parser: NSXMLParser!) {

self.setupItemsOnView()

}

然后我的 setupItemsOnView 函数是:

func setupItemsOnView() {

for (currentElementName) in appParsedData {
println("\(currentElementName):")
}

let test = appParsedData[0] as Dictionary<String, String>
let test_entryid = test["Entry_ID"]
println("Entry ID is \(test_entryid)")

}

呸,好的,基本上我可以在 setupItemsOnView 函数中获取值 Entry_ID、Title、Description、Charge_GBP 和 Charge_USD,但是使用此方法我无法以有意义的方式从分类父项中获取分类和描述值。正如您所看到的那样,我目前正在使用字典来存储数据,所以我想知道当您拥有分层 XML 时使用字典是否正确,但我不知道还能尝试什么。

根据我的背景,您可以使用点符号来引用 XML,并且只需围绕所有元素的父级循环,但我无法快速实现它,所以我只需要一种可用的方法来获取以我可以输出的形式返回所有数据。

如果您提供任何有助于解决此问题的代码,我们将不胜感激。

干杯

D

最佳答案

您想要的是类似于 Swift 的 jaxb 的东西——但我不知道它存在。总之,在 Swift 中解析类似于使用 SAX 解析器。据推测,您的 Swift 代码中有一个类层次结构,它对应于 XML 中的元素层次结构。我使用的方法是有状态的——只需实例化对应于已解析元素的类,并将其保存在为此目的指定的变量中。然后,您的解析代码可以填充在后续回调中遇到的类数据。当然,您必须分层执行此操作,并使用 didEndElement 回调来检测完成。

关于ios - 使用 NSXMLParser 在 Swift 中解析分层 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195136/

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