All Projects → lobodart → Cheatyxml

lobodart / Cheatyxml

Licence: mit
CheatyXML is a Swift framework designed to manage XML easily

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Cheatyxml

Xmlcoder
Easy XML parsing using Codable protocols in Swift
Stars: ✭ 460 (+1900%)
Mutual labels:  xml, cocoapods, xml-parser
Node Xml2js
XML to JavaScript object converter.
Stars: ✭ 4,402 (+19039.13%)
Mutual labels:  xml, xml-parser
Fuzi
A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
Stars: ✭ 894 (+3786.96%)
Mutual labels:  xml, xml-parser
Quick Xml
Rust high performance xml reader and writer
Stars: ✭ 480 (+1986.96%)
Mutual labels:  xml, xml-parser
Tikxml
Modern XML Parser for Android
Stars: ✭ 370 (+1508.7%)
Mutual labels:  xml, xml-parser
Swiftyxmlparser
Simple XML Parser implemented in Swift
Stars: ✭ 413 (+1695.65%)
Mutual labels:  cocoapods, xml-parser
Pulltodismiss
You can dismiss modal viewcontroller like Facebook Messenger by pulling scrollview or navigationbar in Swift.
Stars: ✭ 456 (+1882.61%)
Mutual labels:  swift-framework, cocoapods
Hquery.php
An extremely fast web scraper that parses megabytes of invalid HTML in a blink of an eye. PHP5.3+, no dependencies.
Stars: ✭ 295 (+1182.61%)
Mutual labels:  xml, xml-parser
Swiftinstagram
Instagram API client written in Swift
Stars: ✭ 570 (+2378.26%)
Mutual labels:  swift-framework, cocoapods
Badgehub
A way to quickly add a notification badge icon to any view. Make any view of a full-fledged animated notification center.
Stars: ✭ 592 (+2473.91%)
Mutual labels:  swift-framework, cocoapods
Bluecap
iOS Bluetooth LE framework
Stars: ✭ 669 (+2808.7%)
Mutual labels:  swift-framework, cocoapods
Easyxml
Simplifies parsing and modifying of (huge) XML streams (files) based on the StAX parser with combination of JAXB or JDom2
Stars: ✭ 6 (-73.91%)
Mutual labels:  xml, xml-parser
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+16030.43%)
Mutual labels:  swift-framework, cocoapods
Camaro
camaro is an utility to transform XML to JSON, using Node.js binding to native XML parser pugixml, one of the fastest XML parser around.
Stars: ✭ 438 (+1804.35%)
Mutual labels:  xml, xml-parser
Node Rest Client
REST API client from node.js
Stars: ✭ 365 (+1486.96%)
Mutual labels:  xml, xml-parser
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+3200%)
Mutual labels:  xml, xml-parser
xgen
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
Stars: ✭ 153 (+565.22%)
Mutual labels:  xml, xml-parser
fox
A Fortran XML library
Stars: ✭ 51 (+121.74%)
Mutual labels:  xml, xml-parser
Libexpat
🌿 Expat library: Fast streaming XML parser written in C; in the process of migrating from SourceForge to GitHub
Stars: ✭ 549 (+2286.96%)
Mutual labels:  xml, xml-parser
Koyomi
Simple customizable calendar component in Swift 📆
Stars: ✭ 716 (+3013.04%)
Mutual labels:  swift-framework, cocoapods

CheatyXML

CocoaPods Build Status codecov

CheatyXML is a Swift framework designed to manage XML easily.

Requirements

  • iOS 8.0 or later
  • tvOS 9.0 or later

Installation

Cocoapods

If you're using cocoapods, just add pod 'CheatyXML' into your Podfile file.

Manual

To install this, simply add the .xcodeproj to your project, and do not forget to link the .framework.

Whenever you want to use it in your code, simply type:

import CheatyXML

Usage

Let's take the following XML content for all of our examples:

<blog version="1.0" creator="lobodart">
    <name>MyAwesomeBlog!</name>
    <users>
        <admin is_active="true">lobodart</admin>
        <moderator is_active="false">slash705</moderator>
        <moderator is_active="...">...</moderator>
        ...
    </users>
    <article>
        <title>My first article</title>
        <description>This is the first article</description>
        <infos>
            <date>2015-03-15 15:42:42</date>
            <rate>42</rate>
        </infos>
        ...
    </article>
    <article>
        ...
    </article>
    ...
</blog>

Creating parser instance

Using an URL
let parser: CXMLParser! = CXMLParser(contentsOfURL: ...) // URL
Using a string
let parser: CXMLParser! = CXMLParser(string: ...) // String
Using data
let parser: CXMLParser! = CXMLParser(data: ...) // Data

Retrieving an element using tags

Suppose we want to retrieve the name of our example:

let blogName: String! = parser["name"].stringValue // Returns a String
let blogName: String? = parser["name"].string // Returns an optional String

You can also use the rootElement if you to make your code clearer:

let element = parser.rootElement["name"] // is the same as the notation seen before

To access deeper elements, just chain :

let blogAdmin: String! = parser["users"]["admin"].stringValue
print(blogAdmin) // lobodart

Working with multiple elements

Now let's take a look at the article element. We can see that our blog contains a few articles.

Get an element using its index

If we want to get the title of the first article, we can do it like this:

let firstArticleTitle: String! = parser["article", 0]["title"].stringValue
let firstArticleTitle: String! = parser["article"][0]["title"].stringValue

Both notations have the same effect. Choose the one you like most.

Browse children of an element

To iterate over all children of an element, just use the for in classic syntax:

for element in parser.rootElement {
    print(element.tagName)
}

This code will give us :

name
users
article
article
...

Now, to iterate over specific children of an element, the code is almost the same:

for element in parser.rootElement.elementsNamed("article") {
    print(element.tagName)
}

This time, it will give us :

article
article
...

Of course, you can use this method on any deeper elements (like users for example).

Number of children of an element

If you want to get the total number of children contained in an element, you can use this code:

// Suppose we have 3 moderators in our example
let numberOfElements: Int = parser["users"].numberOfChildElements
print(numberOfElements) // 4 (3 moderators + 1 admin)

Note 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:

let numberOfElements: Int = parser["users"]["moderator"].count
let numberOfElements: Int = parser["users"].elementsNamed("moderator").count

Type casting

CheatyXML allows you to cast tag/attribute values into some common types. You can get either optional or non-optional value for your cast.

let firstArticleRate = parser["article", 0]["rate"]
firstArticleRate.int // Optional(42)
firstArticleRate.intValue // 42
firstArticleRate.float // Optional(42.0)
firstArticleRate.floatValue // 42.0

If 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.

let firstArticleTitle = parser["article", 0]["title"]
firstArticleTitle.string // Optional("My first article")
firstArticleTitle.stringValue // "My first article"
firstArticleTitle.int // nil
firstArticleTitle.intValue // CRASH!

Missing tags

Until now, we always retrieved existing tags but what would happen if a tag doesn't exist? Let's take an example:

let articleDate: String! = parser["article", 0]["infos"]["date"].stringValue
print(articleDate) // 2015-03-15 15:42:42
let articleDateFail: String! = parser["articles", 0]["infos"]["date"].string // I intentionally add an 's' to 'article'
print(articleDateFail) // nil
Note

If 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.

Attributes

Get one

let blogVersion = parser.rootElement.attribute("version")
let adminIsActive = parser["users"]["admin"].attribute("is_active")

You can also use the type casting on attributes:

let blogVersion = parser.rootElement.attribute("version").floatValue // 1.0
let creator = parser.rootElement.attribute("creator").stringValue // "lobodart"

Get all

let attributes = parser.rootElement.attributes // Will give you a [CXMLAttribute]
let dic = attributes.dictionary // Will give you a [String: String]

TO-DO

  • [ ] Add more Unit Tests
  • [ ] Class mapping
  • [ ] XML Generator
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].