ios swift 3 xmlparsing并显示数据

我试图parsing我有的XML数据。 但我不知道我在做什么不能够在屏幕上以及在控制台中打印数据。

为我的api:

Request-type : GET Content-type : application/xml url : www.example.com/data 

这里我的实际结构xml:

 <?xml version='1.0' encoding='UTF-8'?> <map> <data> <list> <item> <map> <answerChoice> <list> <item>option1</item> <item>option2</item> <item>option3</item> <item>option4</item> </list> </answerChoice> <assessmentID>ajhsdja</assessmentID> <explanation>Explanations.</explanation> <question>Which is ur name?</question> <questionType>multiple</questionType> <correctAnswer>option1</correctAnswer> </map> </item> <item> <map> <answerChoice> <list> <item>option1</item> <item>option2</item> <item>option3</item> <item>option4</item> </list> </answerChoice> <assessmentID>ajhsdja</assessmentID> <explanation>Explanations.</explanation> <question>Which is ur name?</question> <questionType>multiple</questionType> <correctAnswer>option1</correctAnswer> </map> </item> <item> <map> <answerChoice> <list> <item>option1</item> <item>option2</item> <item>option3</item> <item>option4</item> </list> </answerChoice> <assessmentID>ajhsdja</assessmentID> <explanation>Explanations.</explanation> <question>Which is ur name?</question> <questionType>multiple</questionType> <correctAnswer>option1</correctAnswer> </map> </item> <item> <map> <answerChoice> <list> <item>option1</item> <item>option2</item> <item>option3</item> <item>option4</item> </list> </answerChoice> <assessmentID>ajhsdja</assessmentID> <explanation>Explanations.</explanation> <question>Which is ur name?</question> <questionType>multiple</questionType> <correctAnswer>option1</correctAnswer> </map> </item> <item> <map> <answerChoice> <list> <item>option1</item> <item>option2</item> <item>option3</item> <item>option4</item> </list> </answerChoice> <assessmentID>ajhsdja</assessmentID> <explanation>Explanations.</explanation> <question>Which is ur name?</question> <questionType>multiple</questionType> <correctAnswer>option1</correctAnswer> </map> </item> </list> </data> </map> 

我试过的代码是:

  import UIKit class ViewController: UIViewController,XMLParserDelegate { var strXMLData:String = "" var currentElement:String = "" var passData:Bool=false var passName:Bool=false var parser = XMLParser() @IBOutlet var questNameData : UILabel! = nil @IBAction var button1 : UIButton! = nil @IBAction var button2 : UIButton! = nil @IBAction var button3 : UIButton! = nil @IBAction var button4 : UIButton! = nil override func viewDidLoad() { super.viewDidLoad() let url:String="http://exampe.com/exc" let urlToSend: URL = URL(string: url)! // Parse the XML parser = XMLParser(contentsOf: urlToSend)! parser.delegate = self let success:Bool = parser.parse() if success { print("parse success!") } else { print("parse failure!") } } func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { } func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { } func parser(_ parser: XMLParser, foundCharacters string: String) { } func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) { } } 

现在我需要显示的屏幕是这样的:

  **QUESTION LABEL ANSWER 1 ANSWER 2 ANSWER 3 ANSWER 4** 

所以我需要显示每个id问题和特定问题的答案。

我试了很多search谷歌之后找不到解决scheme。 任何帮助将是有用的。

提前致谢 !

您可以将您的XML响应转换为JSON格式,如下面的步骤:

首先将这个XMLReader文件添加到你的项目中。 你可以从这里下载: github.com/amarcadet/XMLReader

现在,您必须从您的API获取XML数据

现在创build一个函数来读取json合成器中的xml,

 func ReadXML(xml : String) { do { let dic = try? XMLReader.dictionary(forXMLString: xml) let json = try? JSONSerialization.data(withJSONObject: dic! as [AnyHashable : Any], options: .prettyPrinted) let resDic = try? JSONSerialization.jsonObject(with: json!, options: .allowFragments) print(resDic) } catch { } } 

你可以使用这个function,

  guard let xml = XML(data: YOUR XML DATA RESPONSE) else { return } self.ReadXML(xml: "\(xml)") 

或者你可以通过你的url,

  let url:String="http://synd.cricbuzz.com/j2me/1.0/livematches.xml" let urlToSend: URL = URL(string: url)! guard let xml = XML(contentsOf: urlToSend) else { return } 

希望这会对你有所帮助。 我目前正在处理这个问题。