{"id":18558395,"url":"https://github.com/lobodart/cheatyxml","last_synced_at":"2025-04-10T01:32:46.009Z","repository":{"id":28709861,"uuid":"32230493","full_name":"lobodart/CheatyXML","owner":"lobodart","description":"CheatyXML is a Swift framework designed to manage XML easily","archived":false,"fork":false,"pushed_at":"2021-01-20T13:29:11.000Z","size":90,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T04:47:46.579Z","etag":null,"topics":["cocoapods","ios","swift","swift-framework","xml","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/lobodart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-03-14T20:26:37.000Z","updated_at":"2022-03-31T23:49:56.000Z","dependencies_parsed_at":"2022-09-04T17:42:49.802Z","dependency_job_id":null,"html_url":"https://github.com/lobodart/CheatyXML","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobodart%2FCheatyXML","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobodart%2FCheatyXML/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobodart%2FCheatyXML/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobodart%2FCheatyXML/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lobodart","download_url":"https://codeload.github.com/lobodart/CheatyXML/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248140760,"owners_count":21054351,"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":["cocoapods","ios","swift","swift-framework","xml","xml-parser"],"created_at":"2024-11-06T21:39:41.234Z","updated_at":"2025-04-10T01:32:40.993Z","avatar_url":"https://github.com/lobodart.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CheatyXML\n\n[![CocoaPods](https://img.shields.io/cocoapods/v/CheatyXML.svg)](https://cocoapods.org/pods/CheatyXML)\n[![Build Status](https://travis-ci.org/lobodart/CheatyXML.svg?branch=master)](https://travis-ci.org/lobodart/CheatyXML)\n[![codecov](https://codecov.io/gh/lobodart/CheatyXML/branch/master/graph/badge.svg)](https://codecov.io/gh/lobodart/CheatyXML)\n\nCheatyXML is a Swift framework designed to manage XML easily.\n\n## Requirements\n- iOS 8.0 or later\n- tvOS 9.0 or later\n\n## Installation\n### Cocoapods\nIf you're using **cocoapods**, just add `pod 'CheatyXML'` into your `Podfile` file.\n\n### Manual\nTo install this, simply add the **.xcodeproj** to your project, and do not forget to link the **.framework**.\n\nWhenever you want to use it in your code, simply type:\n```swift\nimport CheatyXML\n```\n\n## Usage\nLet's take the following XML content for all of our examples:\n```xml\n\u003cblog version=\"1.0\" creator=\"lobodart\"\u003e\n    \u003cname\u003eMyAwesomeBlog!\u003c/name\u003e\n    \u003cusers\u003e\n        \u003cadmin is_active=\"true\"\u003elobodart\u003c/admin\u003e\n        \u003cmoderator is_active=\"false\"\u003eslash705\u003c/moderator\u003e\n        \u003cmoderator is_active=\"...\"\u003e...\u003c/moderator\u003e\n        ...\n    \u003c/users\u003e\n    \u003carticle\u003e\n        \u003ctitle\u003eMy first article\u003c/title\u003e\n        \u003cdescription\u003eThis is the first article\u003c/description\u003e\n        \u003cinfos\u003e\n            \u003cdate\u003e2015-03-15 15:42:42\u003c/date\u003e\n            \u003crate\u003e42\u003c/rate\u003e\n        \u003c/infos\u003e\n        ...\n    \u003c/article\u003e\n    \u003carticle\u003e\n        ...\n    \u003c/article\u003e\n    ...\n\u003c/blog\u003e\n```\n\n### Creating parser instance\n\n##### Using an URL\n```swift\nlet parser: CXMLParser! = CXMLParser(contentsOfURL: ...) // URL\n```\n##### Using a string\n```swift\nlet parser: CXMLParser! = CXMLParser(string: ...) // String\n```\n##### Using data\n```swift\nlet parser: CXMLParser! = CXMLParser(data: ...) // Data\n```\n\n### Retrieving an element using tags\nSuppose we want to retrieve the `name` of our example:\n```swift\nlet blogName: String! = parser[\"name\"].stringValue // Returns a String\nlet blogName: String? = parser[\"name\"].string // Returns an optional String\n```\nYou can also use the `rootElement` if you to make your code clearer:\n```swift\nlet element = parser.rootElement[\"name\"] // is the same as the notation seen before\n```\n\nTo access deeper elements, just chain :\n```swift\nlet blogAdmin: String! = parser[\"users\"][\"admin\"].stringValue\nprint(blogAdmin) // lobodart\n```\n\n### Working with multiple elements\nNow let's take a look at the `article` element. We can see that our `blog` contains a few articles.\n\n#### Get an element using its index\nIf we want to get the title of the first article, we can do it like this:\n```swift\nlet firstArticleTitle: String! = parser[\"article\", 0][\"title\"].stringValue\nlet firstArticleTitle: String! = parser[\"article\"][0][\"title\"].stringValue\n```\n\nBoth notations have the same effect. Choose the one you like most.\n\n#### Browse children of an element\nTo iterate over **all** children of an element, just use the `for in` classic syntax:\n```swift\nfor element in parser.rootElement {\n    print(element.tagName)\n}\n```\n\nThis code will give us :\n```\nname\nusers\narticle\narticle\n...\n```\n\nNow, to iterate over **specific** children of an element, the code is almost the same:\n```swift\nfor element in parser.rootElement.elementsNamed(\"article\") {\n    print(element.tagName)\n}\n```\n\nThis time, it will give us :\n```\narticle\narticle\n...\n```\n\nOf course, you can use this method on any deeper elements (like `users` for example).\n\n#### Number of children of an element\nIf you want to get the total number of children contained in an element, you can use this code:\n```swift\n// Suppose we have 3 moderators in our example\nlet numberOfElements: Int = parser[\"users\"].numberOfChildElements\nprint(numberOfElements) // 4 (3 moderators + 1 admin)\n```\n\nNote that this code counts **all** child elements contained in `users`. Now suppose we want to get the number of moderators **only**. There are 2 different syntaxes. Once again, choose your favorite:\n```swift\nlet numberOfElements: Int = parser[\"users\"][\"moderator\"].count\nlet numberOfElements: Int = parser[\"users\"].elementsNamed(\"moderator\").count\n```\n\n### Type casting\nCheatyXML allows you to cast tag/attribute values into some common types. You can get either optional or non-optional value for your cast.\n```swift\nlet firstArticleRate = parser[\"article\", 0][\"rate\"]\nfirstArticleRate.int // Optional(42)\nfirstArticleRate.intValue // 42\nfirstArticleRate.float // Optional(42.0)\nfirstArticleRate.floatValue // 42.0\n```\n\nIf you are not sure about the type, use the optional cast. If you try to cast a value with an inappropriate caster, your app will crash.\n```swift\nlet firstArticleTitle = parser[\"article\", 0][\"title\"]\nfirstArticleTitle.string // Optional(\"My first article\")\nfirstArticleTitle.stringValue // \"My first article\"\nfirstArticleTitle.int // nil\nfirstArticleTitle.intValue // CRASH!\n```\n\n### Missing tags\nUntil now, we always retrieved existing tags but what would happen if a tag doesn't exist? Let's take an example:\n```swift\nlet articleDate: String! = parser[\"article\", 0][\"infos\"][\"date\"].stringValue\nprint(articleDate) // 2015-03-15 15:42:42\nlet articleDateFail: String! = parser[\"articles\", 0][\"infos\"][\"date\"].string // I intentionally add an 's' to 'article'\nprint(articleDateFail) // nil\n```\n\u003e ###### Note\nIf you have any doubt, keep in mind that using `.string` is safer than using `.stringValue`. In the previous example, using `.stringValue` on `articleDateFail` will result in your application to crash.\n\n\n### Attributes\n#### Get one\n```swift\nlet blogVersion = parser.rootElement.attribute(\"version\")\nlet adminIsActive = parser[\"users\"][\"admin\"].attribute(\"is_active\")\n```\n\nYou can also use the type casting on attributes:\n```swift\nlet blogVersion = parser.rootElement.attribute(\"version\").floatValue // 1.0\nlet creator = parser.rootElement.attribute(\"creator\").stringValue // \"lobodart\"\n```\n\n#### Get all\n```swift\nlet attributes = parser.rootElement.attributes // Will give you a [CXMLAttribute]\nlet dic = attributes.dictionary // Will give you a [String: String]\n```\n\n### TO-DO\n- [ ] Add more Unit Tests\n- [ ] Class mapping\n- [ ] XML Generator\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flobodart%2Fcheatyxml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flobodart%2Fcheatyxml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flobodart%2Fcheatyxml/lists"}