{"id":22690004,"url":"https://github.com/yahoojapan/swiftyxmlparser","last_synced_at":"2025-05-16T14:04:54.303Z","repository":{"id":8199384,"uuid":"57187184","full_name":"yahoojapan/SwiftyXMLParser","owner":"yahoojapan","description":"Simple XML Parser implemented in Swift","archived":false,"fork":false,"pushed_at":"2023-01-18T23:35:59.000Z","size":297,"stargazers_count":591,"open_issues_count":9,"forks_count":93,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-05-16T14:04:52.329Z","etag":null,"topics":["carthage","cocoapods","ios","swift","xml-parser"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yahoojapan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-27T05:53:44.000Z","updated_at":"2025-04-30T05:40:17.000Z","dependencies_parsed_at":"2023-02-10T19:00:37.822Z","dependency_job_id":null,"html_url":"https://github.com/yahoojapan/SwiftyXMLParser","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoojapan%2FSwiftyXMLParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoojapan%2FSwiftyXMLParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoojapan%2FSwiftyXMLParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoojapan%2FSwiftyXMLParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yahoojapan","download_url":"https://codeload.github.com/yahoojapan/SwiftyXMLParser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254544146,"owners_count":22088807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["carthage","cocoapods","ios","swift","xml-parser"],"created_at":"2024-12-10T00:25:03.978Z","updated_at":"2025-05-16T14:04:54.280Z","avatar_url":"https://github.com/yahoojapan.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"![swiftyxmlparserlogo](https://user-images.githubusercontent.com/18320004/31585849-abf82a6a-b203-11e7-9494-007cebd29aa6.png)\n\n![Swift 5.0](https://img.shields.io/badge/Swift-5.0-orange.svg)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Version](https://img.shields.io/cocoapods/v/SwiftyXMLParser.svg?style=flat)](http://cocoapods.org/pods/SwiftyXMLParser)\n[![License](https://img.shields.io/cocoapods/l/SwiftyXMLParser.svg?style=flat)](http://cocoapods.org/pods/SwiftyXMLParser)\n ![Platform](https://img.shields.io/badge/platforms-iOS%209.0+%20%7C%20macOS%2010.10+%20%7C%20tvOS%209.0+-333333.svg)\n\nSimple XML Parser implemented in Swift\n\n# What's this?\nThis is a XML parser inspired by [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) and  [SWXMLHash](https://github.com/drmohundro/SWXMLHash).\n\n[NSXMLParser](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/) in Foundation framework is a kind of \"SAX\" parser. It has enough performance but is a little inconvenient. So we have implemented \"DOM\" parser wrapping it. \n\n# Feature\n- [x] access XML Document with \"subscript\".\n- [x] access XML Document as Sequence.\n- [x] easy debugging XML pathes.\n\n# Requirement\n+ iOS 9.0+\n+ tvOS 9.0+\n+ macOS 10.10+\n+ Swift 5.0\n\n# Installation\n\n### Carthage\n#### 1. create Cartfile\n\n```ruby:Cartfile\ngithub \"https://github.com/yahoojapan/SwiftyXMLParser\"\n\n```\n#### 2. install\n```\n\u003e carthage update\n```\n\n### CocoaPods\n#### 1. create Podfile\n```ruby:Podfile\nplatform :ios, '9.0'\nuse_frameworks!\n\npod \"SwiftyXMLParser\", :git =\u003e 'https://github.com/yahoojapan/SwiftyXMLParser.git'\n```\n\n#### 2. install\n```\n\u003e pod install\n```\n\n# Example\n\n```swift\nimport SwiftyXMLParser\n\nlet str = \"\"\"\n\u003cResultSet\u003e\n    \u003cResult\u003e\n        \u003cHit index=\\\"1\\\"\u003e\n            \u003cName\u003eItem1\u003c/Name\u003e\n        \u003c/Hit\u003e\n        \u003cHit index=\\\"2\\\"\u003e\n            \u003cName\u003eItem2\u003c/Name\u003e\n        \u003c/Hit\u003e\n    \u003c/Result\u003e\n\u003c/ResultSet\u003e\n\"\"\"\n\n// parse xml document\nlet xml = try! XML.parse(str)\n\n// access xml element\nlet accessor = xml[\"ResultSet\"]\n\n// access XML Text\n\nif let text = xml[\"ResultSet\", \"Result\", \"Hit\", 0, \"Name\"].text {\n    print(text)\n}\n\nif let text = xml.ResultSet.Result.Hit[0].Name.text {\n    print(text)\n}\n\n// access XML Attribute\nif let index = xml[\"ResultSet\", \"Result\", \"Hit\", 0].attributes[\"index\"] {\n    print(index)\n}\n\n// enumerate child Elements in the parent Element\nfor hit in xml[\"ResultSet\", \"Result\", \"Hit\"] {\n    print(hit)\n}\n\n// check if the XML path is wrong\nif case .failure(let error) =  xml[\"ResultSet\", \"Result\", \"TypoKey\"] {\n    print(error)\n}\n```\n\n# Usage\n### 1. Parse XML\n+ from String\n```swift\nlet str = \"\"\"\n\u003cResultSet\u003e\n    \u003cResult\u003e\n        \u003cHit index=\\\"1\\\"\u003e\n            \u003cName\u003eItem1\u003c/Name\u003e\n        \u003c/Hit\u003e\n        \u003cHit index=\\\"2\\\"\u003e\n            \u003cName\u003eItem2\u003c/Name\u003e\n        \u003c/Hit\u003e\n    \u003c/Result\u003e\n\u003c/ResultSet\u003e\n\"\"\"\n\nxml = try! XML.parse(str) // -\u003e XML.Accessor\n```\n+ from NSData\n```swift\nlet str = \"\"\"\n\u003cResultSet\u003e\n    \u003cResult\u003e\n        \u003cHit index=\\\"1\\\"\u003e\n            \u003cName\u003eItem1\u003c/Name\u003e\n        \u003c/Hit\u003e\n        \u003cHit index=\\\"2\\\"\u003e\n            \u003cName\u003eItem2\u003c/Name\u003e\n        \u003c/Hit\u003e\n    \u003c/Result\u003e\n\u003c/ResultSet\u003e\n\"\"\"\n\nlet string = String(decoding: data, as: UTF8.self)\n\nxml = XML.parse(data) // -\u003e XML.Accessor\n```\n\n+ with invalid character\n\n```swift\nlet srt = \"\u003cxmlopening\u003e@ß123\\u{1c}\u003c/xmlopening\u003e\"\n\nlet xml = XML.parse(str.data(using: .utf8))\n\nif case .failure(XMLError.interruptedParseError) = xml {\n  print(\"invalid character\")\n}\n\n```\n\nFor more, see https://developer.apple.com/documentation/foundation/xmlparser/errorcode \n\n\n### 2. Access child Elements\n```swift\nlet element = xml.ResultSet // -\u003e XML.Accessor\n```\n\n### 3. Access grandchild Elements\n+ with String\n```swift\nlet element = xml[\"ResultSet\"][\"Result\"] // -\u003e \u003cResult\u003e\u003cHit index=\\\"1\\\"\u003e\u003cName\u003eItem1\u003c/Name\u003e\u003c/Hit\u003e\u003cHit index=\\\"2\\\"\u003e\u003cName\u003eItem2\u003c/Name\u003e\u003c/Hit\u003e\u003c/Result\u003e\n```\n+ with Array\n```swift\nlet path = [\"ResultSet\", \"Result\"]\nlet element = xml[path] // -\u003e \u003cResult\u003e\u003cHit index=\\\"1\\\"\u003e\u003cName\u003eItem1\u003c/Name\u003e\u003c/Hit\u003e\u003cHit index=\\\"2\\\"\u003e\u003cName\u003eItem2\u003c/Name\u003e\u003c/Hit\u003e\u003c/Result\u003e\n```\n+ with Variadic\n```swift\nlet element = xml[\"ResultSet\", \"Result\"] // -\u003e \u003cResult\u003e\u003cHit index=\\\"1\\\"\u003e\u003cName\u003eItem1\u003c/Name\u003e\u003c/Hit\u003e\u003cHit index=\\\"2\\\"\u003e\u003cName\u003eItem2\u003c/Name\u003e\u003c/Hit\u003e\u003c/Result\u003e\n```\n+ with @dynamicMemberLookup\n```swift\nlet element = xml.ResultSet.Result // -\u003e \u003cResult\u003e\u003cHit index=\\\"1\\\"\u003e\u003cName\u003eItem1\u003c/Name\u003e\u003c/Hit\u003e\u003cHit index=\\\"2\\\"\u003e\u003cName\u003eItem2\u003c/Name\u003e\u003c/Hit\u003e\u003c/Result\u003e\n```\n### 4. Access specific grandchild Element\n```swift\nlet element = xml.ResultSet.Result.Hit[1] // -\u003e \u003cHit index=\\\"2\\\"\u003e\u003cName\u003eItem2\u003c/Name\u003e\u003c/Hit\u003e\n```\n### 5. Access attribute in Element\n```swift\nif let attributeValue = xml.ResultSet.Result.Hit[1].attributes?[\"index\"] {\n  print(attributeValue) // -\u003e 2\n}\n```\n### 6. Access text in Element\n+ with optional binding\n```swift\nif let text = xml.ResultSet.Result.Hit[1].Name.text {\n    print(text) // -\u003e Item2\n} \n```\n+ with custom operation\n```swift\nstruct Entity {\n  var name = \"\"\n}\nlet entity = Entity()\nentity.name ?= xml.ResultSet.Result.Hit[1].Name.text // assign if it has text\n```\n+ convert Int and assign\n```swift\nstruct Entity {\n  var name: Int = 0\n}\nlet entity = Entity()\nentity.name ?= xml.ResultSet.Result.Hit[1].Name.int // assign if it has Int\n```\nand there are other syntax sugers, bool, url and double.\n+ assign text into Array\n```swift\nstruct Entity {\n  var names = [String]()\n}\nlet entity = Entity()\nentity.names ?\u003c\u003c xml.ResultSet.Result.Hit[1].Name.text // assign if it has text\n```\n\n### 7. Access CDATA\n```swift\nlet str = \"\"\"\n\u003cData name=\"DATE\"\u003e\n    \u003cvalue\u003e\u003c![CDATA[2018-07-08]]\u003e\u003c/value\u003e\n\u003c/Data\u003e\n\"\"\"\n\n// parse xml document\nlet xml = try! XML.parse(str)\n        \nif let cdata = xml.Data.value.element?.CDATA, \n   let cdataStr = String(data: cdata, encoding: .utf8) {\n   print(cdataStr) // -\u003e \"2018-07-08\"\n}\n```\n\n### 7. Count child Elements\n```swift\nlet numberOfHits = xml.ResultSet.Result.Hit.all?.count \n```\n### 8. Check error\n```swift\nprint(xml.ResultSet.Result.TypoKey) // -\u003e \"TypoKey not found.\"\n```\n\n### 9. Access as SequenceType\n+ for-in\n```swift\nfor element in xml.ResultSet.Result.Hit {\n  print(element.text)\n}\n```\n+ map\n```swift\nxml.ResultSet.Result.Hit.map { $0.Name.text }\n```\n\n### 10. Generate XML document\n```swift\nprint(Converter(xml.ResultSet).makeDocument())\n```\n\n\n## Work with Alamofire\nSwiftyXMLParser goes well with [Alamofire](https://github.com/Alamofire/Alamofire). You can parse the response easily.\n\n```swift\nimport Alamofire\nimport SwiftyXMLParser\n\nAlamofire.request(.GET, \"https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml\")\n         .responseData { response in\n            if let data = response.data {\n                let xml = XML.parse(data)\n                print(xml.feed.entry[0].title.text) // outputs the top title of iTunes app raning.\n            }\n        }\n```\n\nIn addition, there is the extension of Alamofire to combine with SwiftyXMLParser. \n\n* [Alamofire-SwiftyXMLParser](https://github.com/kazuhiro4949/Alamofire-SwiftyXMLParser)\n\n# Migration Guide\n[Current master branch](https://github.com/yahoojapan/SwiftyXMLParser/tree/master) is supporting Xcode10.\nIf you wanna use this library with legacy swift version, read [release notes](https://github.com/yahoojapan/SwiftyXMLParser/releases) and install the last compatible version.\n\n# License\n\nThis software is released under the MIT License, see LICENSE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyahoojapan%2Fswiftyxmlparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyahoojapan%2Fswiftyxmlparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyahoojapan%2Fswiftyxmlparser/lists"}