{"id":3245,"date":"2021-06-23T17:11:45","date_gmt":"2021-06-23T09:11:45","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3245"},"modified":"2021-06-23T17:11:45","modified_gmt":"2021-06-23T09:11:45","slug":"swift-%e4%bd%bf%e7%94%a8%e5%8e%9f%e7%94%9f%e7%9a%84xmlparser%e7%b1%bb%e6%9d%a5%e8%a7%a3%e6%9e%90xml%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3245","title":{"rendered":"Swift &#8211; \u4f7f\u7528\u539f\u751f\u7684XMLParser\u7c7b\u6765\u89e3\u6790xml\u6570\u636e"},"content":{"rendered":"\n<p>\u6709\u65f6\u6211\u4eec\u7684\u5e94\u7528\u9700\u8981\u89e3\u6790&nbsp;<strong>XML&nbsp;<\/strong>\u6570\u636e\uff0c\u6bd4\u5982\u83b7\u53d6&nbsp;<strong>Web Service&nbsp;<\/strong>\u6570\u636e\u3001\u8bfb\u53d6&nbsp;<strong>RSS&nbsp;<\/strong>\u65b0\u95fb\u6216\u8005\u535a\u5ba2\u6570\u636e\u6e90\u3002\u6211\u4e4b\u524d\u4e5f\u5199\u8fc7\u6587\u7ae0\u4ecb\u7ecd\u5982\u4f55\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\u6765\u89e3\u6790&nbsp;<strong>XML&nbsp;<\/strong>\u683c\u5f0f\u6570\u636e\u3002<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_646.html\" target=\"_blank\" rel=\"noreferrer noopener\">Swift &#8211; \u89e3\u6790XML\u683c\u5f0f\u6570\u636e\uff08\u5206\u522b\u4f7f\u7528GDataXML\u548cKissXML\uff09<\/a><\/li><\/ul>\n\n\n\n<p>\u00a0\u5176\u5b9e\u82f9\u679c\u539f\u751f\u5c31\u63d0\u4f9b\u4e86\u00a0<strong>XMLParser\u00a0<\/strong>\u7c7b\u6765\u89e3\u6790\u00a0<strong>XML\u00a0<\/strong>\u6587\u6863\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u518d\u5f15\u5165\u5176\u5b83\u7b2c\u4e09\u65b9\u5e93\uff08\u5f53\u7136\u7528\u8d77\u6765\u4e0e\u4e0a\u9762\u8fd9\u4e24\u4e2a\u7b2c\u4e09\u65b9\u5e93\u76f8\u6bd4\uff0c\u4f1a\u9ebb\u70e6\u4e9b\uff09\uff0c\u4e0b\u9762\u901a\u8fc7\u6837\u4f8b\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n\n\n\n<p>1\uff0c\u6548\u679c\u56fe<\/p>\n\n\n\n<p>\uff081\uff09\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u88ab\u89e3\u6790\u7684\u00a0<strong>XML\u00a0<\/strong>\u6570\u636e\u6587\u4ef6\uff08<strong>users.xml<\/strong>\uff09\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-68-1024x450.png\" alt=\"\" class=\"wp-image-3246\" width=\"590\" height=\"259\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-1024x450.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-300x132.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-768x338.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-1536x675.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-830x365.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-230x101.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-350x154.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68-480x211.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-68.png 1560w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\u9700\u8981\u5c06\u5176\u89e3\u6790\u51fa\u6765\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u4e2d\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-69.png\" alt=\"\" class=\"wp-image-3247\" width=\"614\" height=\"154\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69.png 862w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-300x75.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-768x192.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-830x208.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-230x58.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-350x88.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-69-480x120.png 480w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/figure><\/div>\n\n\n\n<p>2\uff0c\u6837\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n \n\/\/\u7528\u6237\u5bf9\u8c61\nstruct User {\n    var id: String?  \/\/\u7f16\u53f7\n    var name: String? \/\/\u59d3\u540d\n    var mobile: String? \/\/\u624b\u673a\n    var home: String?  \/\/\u56fa\u8bdd\n}\n \nclass ViewController: UIViewController,XMLParserDelegate {\n    \/\/\u4fdd\u5b58\u6700\u7ec8\u89e3\u6790\u7684\u7ed3\u679c\n    var users:[User] = []\n     \n    \/\/\u5f53\u524d\u5143\u7d20\u540d\n    var currentElement = \"\"\n     \n    \/\/\u5f53\u524d\u7528\u6237\n    var user:User!\n \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u83b7\u53d6xml\u6587\u4ef6\u8def\u5f84\n        let file = Bundle.main.path(forResource: \"users\", ofType: \"xml\")\n        \/\/\u521d\u59cb\u5316parser\n        let parser = XMLParser(contentsOf: NSURL(fileURLWithPath: file!) as URL)!\n\n        \/\/ \u4e0a\u9762\u662f\u6587\u4ef6\uff0c\u4e0b\u9762\u662f\u5b57\u7b26\u4e32\u7684\u4f8b\u5b50\uff0c\u628axml\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u89e3\u6790\n        \/\/let parser = XMLParser.init(data: dataString.data(using: .utf8)!)\n\n        \/\/\u8bbe\u7f6edelegate\n        parser.delegate = self\n        \/\/\u5f00\u59cb\u89e3\u6790\n        parser.parse()\n        \/\/\u8f93\u51fa\u7ed3\u679c\n        for i in 0...(users.count - 1 ){\n            print(\"User: id:\\(users[i].id!)\uff0cname:\\(users[i].name!)\uff0c\"\n                + \"mobile:\\(users[i].mobile!)\uff0chome:\\(users[i].home!)\")\n        }\n    }\n     \n    \/\/ \u9047\u5230\u4e00\u4e2a\u5f00\u59cb\u6807\u7b7e\u65f6\u8c03\u7528\n    func parser(_ parser: XMLParser, didStartElement elementName: String,\n                namespaceURI: String?, qualifiedName qName: String?,\n                attributes attributeDict: [String : String] = [:]) {\n        currentElement = elementName\n        \/\/\u5f53elementName\u662f \"User\"\u65f6\uff0c\u8868\u793a\u5f00\u59cb\u89e3\u6790\u4e00\u4e2a\u65b0\u7528\u6237\u4e86\n        if elementName == \"User\"{\n            \/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u7528\u6237\u5bf9\u8c61\n            user = User()\n            \/\/\u4fdd\u5b58\u4e0bid\n            user.id = attributeDict[\"id\"]\n        }\n    }\n     \n    \/\/ \u9047\u5230\u5b57\u7b26\u4e32\u65f6\u8c03\u7528\n    func parser(_ parser: XMLParser, foundCharacters string: String) {\n        let data = string.trimmingCharacters(in: .whitespacesAndNewlines)\n        \/\/\u63a5\u4e0b\u6765\u6bcf\u9047\u5230\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c06\u8be5\u5b57\u7b26\u8ffd\u52a0\u5230\u76f8\u5e94\u7684 property \u4e2d\n        switch currentElement{\n        case \"name\":\n            user.name = user.name ?? \"\" + data\n        case \"mobile\":\n            user.mobile = user.mobile ?? \"\" +  data\n        case \"home\":\n            user.home = user.home ?? \"\" + data\n        default:\n            break\n        }\n    }\n     \n    \/\/ \u9047\u5230\u7ed3\u675f\u6807\u7b7e\u65f6\u8c03\u7528\n    func parser(_ parser: XMLParser, didEndElement elementName: String,\n                namespaceURI: String?, qualifiedName qName: String?) {\n        \/\/\u6807\u7b7eUser\u7ed3\u675f\u65f6\u5c06\u8be5\u7528\u6237\u5bf9\u8c61\uff0c\u5b58\u5165\u6570\u7ec4\u5bb9\u5668\u3002\n        if elementName == \"User\"{\n            users.append(user)\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u6211\u4eec\u7684\u5e94\u7528\u9700\u8981\u89e3\u6790&nbsp;XML&nbsp;\u6570\u636e\uff0c\u6bd4\u5982\u83b7\u53d6&nbsp;Web Service&#038;nbsp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3245"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3245"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3245\/revisions"}],"predecessor-version":[{"id":3248,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3245\/revisions\/3248"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}