gpt4 book ai didi

xml - Swift xml 解析器导致错误

转载 作者:行者123 更新时间:2023-11-30 12:21:47 25 4
gpt4 key购买 nike

我正在尝试解析一个简单的 xml 文件,但我不断收到错误

"Error Description:The operation couldn’t be completed. (Cocoa error -1.) Line number: 0"

XML 文件(名为 mathquiz.xml,放置在名为 img 的文件夹中):

<quiz>
<title>
Y1 Maths Quiz
</title>
<question>
<description>
What is 2+2
</description>
<answer>
4
</answer>
<answer>
3
</answer>
<answer>
2
</answer>
<answer>
1
</answer>
</question>
</quiz>

解析器:

//
// QuizXMLParser.swift
// MathQuiz
//
// Created by Raees on 22/06/2017.
// Copyright © 2017 Raees Apps. All rights reserved.
//

import Foundation

public class QuizXMLParser : NSObject, XMLParserDelegate {

var currentContent = ""
var quiz = Quiz()
var question = Question()

public func beginParsing(file url: String) {
guard let myURL = NSURL(string:url) else {
print("URL not defined properly")
return
}
guard let parser = XMLParser(contentsOf: myURL as URL) else {
print("Cannot Read Data")
return
}
parser.delegate = self
if !parser.parse(){
print("Data Errors Exist:")
let error = parser.parserError!
print("Error Description:\(error.localizedDescription)")
print("Line number: \(parser.lineNumber)")
}
}

public func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]){
print("Beginning tag: <\(elementName)>")
if elementName == "question" {
question = Question()
}
currentContent = ""
}

public func parser(_ parser: XMLParser, foundCharacters string: String){
currentContent += string
print("Added to make \(currentContent)")
}

public func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?){
print("ending tag: </\(elementName)> with contents:\(currentContent)")
switch elementName{
case "question":
quiz.questions.append(question)
print ("model has \(question)")
case"title":
quiz.title = currentContent
case "description":
if let index = Int(currentContent) {
quiz.currentQuestionIndex = index
}
case "answer":
question.answers.append(currentContent)
default:
return
}
}
}

在 View Controller 中调用解析器:

let parser = QuizXMLParser()
parser.beginParsing(file: "img/mathquiz.xml")

怎样才能让它成功解析XML文件?我没觉得有什么问题。

最佳答案

首先,为了从 Bundle 的内容中读取文件,请执行以下操作:

    let parser = QuizXMLParser()
if let path = Bundle.main.url(forResource: "mathQuiz", withExtension: "xml") {
parser.beginParsing(file: path)
}

然后更改函数 beginParsing(file url: String) 以接受 URL 而不是字符串。

public func beginParsing(file url: URL) {
guard let parser = XMLParser(contentsOf: url) else {
print("Cannot Read Data")
return
}
parser.delegate = self
if !parser.parse(){
print("Data Errors Exist:")
let error = parser.parserError!
print("Error Description:\(error.localizedDescription)")
print("Line number: \(parser.lineNumber)")
}
}

这应该可以解决您的问题。

关于xml - Swift xml 解析器导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44708753/

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