All Projects → yahoojapan → Swiftyxmlparser

yahoojapan / Swiftyxmlparser

Licence: mit
Simple XML Parser implemented in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftyxmlparser

Xmlcoder
Easy XML parsing using Codable protocols in Swift
Stars: ✭ 460 (+11.38%)
Mutual labels:  cocoapods, carthage, xml-parser
Gzipswift
Swift framework that enables gzip/gunzip Data using zlib
Stars: ✭ 356 (-13.8%)
Mutual labels:  cocoapods, carthage
Gradientcircularprogress
Customizable progress indicator library in Swift
Stars: ✭ 407 (-1.45%)
Mutual labels:  cocoapods, carthage
Circularprogress
Circular progress indicator for your macOS app
Stars: ✭ 366 (-11.38%)
Mutual labels:  cocoapods, carthage
Pagecontroller
Infinite paging controller, scrolling through contents and title bar scrolls with a delay
Stars: ✭ 344 (-16.71%)
Mutual labels:  cocoapods, carthage
Observable
The easiest way to observe values in Swift.
Stars: ✭ 346 (-16.22%)
Mutual labels:  cocoapods, carthage
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 (+798.31%)
Mutual labels:  cocoapods, carthage
Rxappstate
RxSwift extensions for UIApplicationDelegate methods to observe changes in your app's state
Stars: ✭ 328 (-20.58%)
Mutual labels:  cocoapods, carthage
Restofire
Restofire is a protocol oriented networking client for Alamofire
Stars: ✭ 377 (-8.72%)
Mutual labels:  cocoapods, carthage
Vgplayer
📺 A simple iOS video player by Vein.
Stars: ✭ 383 (-7.26%)
Mutual labels:  cocoapods, carthage
Viperit
Viper Framework for iOS using Swift
Stars: ✭ 404 (-2.18%)
Mutual labels:  cocoapods, carthage
Sclalertview
Beautiful animated Alert View. Written in Objective-C
Stars: ✭ 3,426 (+729.54%)
Mutual labels:  cocoapods, carthage
Persei
Animated top menu for UITableView / UICollectionView / UIScrollView written in Swift
Stars: ✭ 3,395 (+722.03%)
Mutual labels:  cocoapods, carthage
Taggerkit
🏷 TaggerKit helps you to quickly implement tags in your UIKit apps, so you can go on and test your idea without having to worry about logic and custom collection layouts.
Stars: ✭ 390 (-5.57%)
Mutual labels:  cocoapods, carthage
Tbuiautotest
Generating UI test label automatically for iOS.
Stars: ✭ 333 (-19.37%)
Mutual labels:  cocoapods, carthage
Ssspinnerbutton
Forget about typical stereotypic loading, It's time to change. SSSpinnerButton is an elegant button with a diffrent spinner animations.
Stars: ✭ 357 (-13.56%)
Mutual labels:  cocoapods, carthage
Coding Ios
CODING iOS 客户端源代码
Stars: ✭ 3,771 (+813.08%)
Mutual labels:  cocoapods, carthage
Serrata
Slide image viewer library similar to Twitter and LINE.
Stars: ✭ 322 (-22.03%)
Mutual labels:  cocoapods, carthage
Maplebacon
🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations
Stars: ✭ 322 (-22.03%)
Mutual labels:  cocoapods, carthage
Foldingtabbar.ios
Folding Tab Bar and Tab Bar Controller
Stars: ✭ 3,677 (+790.31%)
Mutual labels:  cocoapods, carthage

swiftyxmlparserlogo

Swift 5.0 Carthage compatible Version License Platform

Simple XML Parser implemented in Swift

What's this?

This is a XML parser inspired by SwiftyJSON and SWXMLHash.

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

Feature

  • [x] access XML Document with "subscript".
  • [x] access XML Document as Sequence.
  • [x] easy debugging XML pathes.

Requirement

  • iOS 9.0+
  • tvOS 9.0+
  • macOS 10.10+
  • Swift 5.0

Installation

Carthage

1. create Cartfile

github "https://github.com/yahoojapan/SwiftyXMLParser"

2. install

> carthage update

CocoaPods

1. create Podfile

platform :ios, '9.0'
use_frameworks!

pod "SwiftyXMLParser", :git => 'https://github.com/yahoojapan/SwiftyXMLParser.git'

2. install

> pod install

Example

import SwiftyXMLParser

let str = """
<ResultSet>
    <Result>
        <Hit index=\"1\">
            <Name>Item1</Name>
        </Hit>
        <Hit index=\"2\">
            <Name>Item2</Name>
        </Hit>
    </Result>
</ResultSet>
"""

// parse xml document
let xml = try! XML.parse(str)

// access xml element
let accessor = xml["ResultSet"]

// access XML Text

if let text = xml["ResultSet", "Result", "Hit", 0, "Name"].text {
    print(text)
}

if let text = xml.ResultSet.Result.Hit[0].Name.text {
    print(text)
}

// access XML Attribute
if let index = xml["ResultSet", "Result", "Hit", 0].attributes["index"] {
    print(index)
}

// enumerate child Elements in the parent Element
for hit in xml["ResultSet", "Result", "Hit"] {
    print(hit)
}

// check if the XML path is wrong
if case .failure(let error) =  xml["ResultSet", "Result", "TypoKey"] {
    print(error)
}

Usage

1. Parse XML

  • from String
let str = """
<ResultSet>
    <Result>
        <Hit index=\"1\">
            <Name>Item1</Name>
        </Hit>
        <Hit index=\"2\">
            <Name>Item2</Name>
        </Hit>
    </Result>
</ResultSet>
"""

xml = try! XML.parse(str) // -> XML.Accessor
  • from NSData
let str = """
<ResultSet>
    <Result>
        <Hit index=\"1\">
            <Name>Item1</Name>
        </Hit>
        <Hit index=\"2\">
            <Name>Item2</Name>
        </Hit>
    </Result>
</ResultSet>
"""

let string = String(decoding: data, as: UTF8.self)

xml = XML.parse(data) // -> XML.Accessor
  • with invalid character
let srt = "<xmlopening>@ß123\u{1c}</xmlopening>"

let xml = XML.parse(str.data(using: .utf8))

if case .failure(XMLError.interruptedParseError) = xml {
  print("invalid character")
}

For more, see https://developer.apple.com/documentation/foundation/xmlparser/errorcode

2. Access child Elements

let element = xml.ResultSet // -> XML.Accessor

3. Access grandchild Elements

  • with String
let element = xml["ResultSet"]["Result"] // -> <Result><Hit index=\"1\"><Name>Item1</Name></Hit><Hit index=\"2\"><Name>Item2</Name></Hit></Result>
  • with Array
let path = ["ResultSet", "Result"]
let element = xml[path] // -> <Result><Hit index=\"1\"><Name>Item1</Name></Hit><Hit index=\"2\"><Name>Item2</Name></Hit></Result>
  • with Variadic
let element = xml["ResultSet", "Result"] // -> <Result><Hit index=\"1\"><Name>Item1</Name></Hit><Hit index=\"2\"><Name>Item2</Name></Hit></Result>
  • with @dynamicMemberLookup
let element = xml.ResultSet.Result // -> <Result><Hit index=\"1\"><Name>Item1</Name></Hit><Hit index=\"2\"><Name>Item2</Name></Hit></Result>

4. Access specific grandchild Element

let element = xml.ResultSet.Result.Hit[1] // -> <Hit index=\"2\"><Name>Item2</Name></Hit>

5. Access attribute in Element

if let attributeValue = xml.ResultSet.Result.Hit[1].attributes?["index"] {
  print(attributeValue) // -> 2
}

6. Access text in Element

  • with optional binding
if let text = xml.ResultSet.Result.Hit[1].Name.text {
    print(text) // -> Item2
} 
  • with custom operation
struct Entity {
  var name = ""
}
let entity = Entity()
entity.name ?= xml.ResultSet.Result.Hit[1].Name.text // assign if it has text
  • convert Int and assign
struct Entity {
  var name: Int = 0
}
let entity = Entity()
entity.name ?= xml.ResultSet.Result.Hit[1].Name.int // assign if it has Int

and there are other syntax sugers, bool, url and double.

  • assign text into Array
struct Entity {
  var names = [String]()
}
let entity = Entity()
entity.names ?<< xml.ResultSet.Result.Hit[1].Name.text // assign if it has text

7. Count child Elements

let numberOfHits = xml.ResultSet.Result.Hit.all?.count 

8. Check error

print(xml.ResultSet.Result.TypoKey) // -> "TypoKey not found."

9. Access as SequenceType

  • for-in
for element in xml.ResultSet.Result.Hit {
  print(element.text)
}
  • map
xml.ResultSet.Result.Hit.map { $0.Name.text }

9. Generate XML document

print(Converter(xml.ResultSet).makeDocument())

Work with Alamofire

SwiftyXMLParser goes well with Alamofire. You can parse the response easily.

import Alamofire
import SwiftyXMLParser

Alamofire.request(.GET, "https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml")
         .responseData { response in
            if let data = response.data {
                let xml = XML.parse(data)
                print(xml.feed.entry[0].title.text) // outputs the top title of iTunes app raning.
            }
        }

In addition, there is the extension of Alamofire to combine with SwiftyXMLParser.

Migration Guide

Current master branch is supporting Xcode10. If you wanna use this library with legacy swift version, read release notes and install the last compatible version.

License

This software is released under the MIT License, see LICENSE.

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