我真的很难以我可以实际使用的形式获取分层 XML 值,因此非常感谢任何帮助。我对 Swift 和 IOS 开发还很陌生,所以说实话我并不完全理解解析器,但我希望在这之后我会的!
这是我试图解析的示例 XML,它来自一个肥皂网络服务。在连接和获取数据方面的所有方面都工作得很好
<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、Title、Description、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