ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - chenyunguiMilook/SwiftyXML: The most swifty way to deal with XML data in swift 5.
The most swifty way to deal with XML data in swift 5. - chenyunguiMilook/SwiftyXML
Visit Site

GitHub - chenyunguiMilook/SwiftyXML: The most swifty way to deal with XML data in swift 5.

GitHub - chenyunguiMilook/SwiftyXML: The most swifty way to deal with XML data in swift 5.

Carthage compatible

SwiftyXML

Platform

SwiftyXML use most swifty way to deal with XML data.

Features

  • Infinity subscript
  • dynamicMemberLookup Support (use $ started string to subscript attribute)
  • Optional | Non-optional value access
  • Directly access Enum type value (enums extends from RawRepresentable)
  • Directly for loop in XML children nodes
  • Accurate error throwing
  • XML construct, formatting
  • Single source file

Sample XML:

<catalog>
	<product description="Cardigan Sweater" product_image="cardigan.jpg" >
		<catalog_item gender="Men's" >
			<item_number>QWZ5671</item_number>
			<price>39.95</price>
			<size description="Medium" >
				<color_swatch image="red_cardigan.jpg" >Red</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg" >Burgundy</color_swatch>
			</size>
			<size description="Large" >
				<color_swatch image="red_cardigan.jpg" >Red</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg" >Burgundy</color_swatch>
			</size>
		</catalog_item>
		<catalog_item gender="Women's" >
			<item_number>RRX9856</item_number>
			<price>42.50</price>
			<size description="Small" >
				<color_swatch image="red_cardigan.jpg" >Red</color_swatch>
				<color_swatch image="navy_cardigan.jpg" >Navy</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg" >Burgundy</color_swatch>
			</size>
		</catalog_item>
	</product>
</catalog>

With SwiftyXML all you have to do is:

let xml = XML(string: xmlContent)
let color0 = xml.product.catalog_item.size.color_swatch.1.string //"Burgundy"
// notice that, we use "$" prefix for subscript attribute
let description0 = xml.product.catalog_item.size.1.$description.string //"Large"

This is same as below, SwiftyXML will auto pick the first element as default:

let xml = XML(data: xmlFileData)
let color = xml.product.0.catalog_item.0.size.0.color_swatch.1.string //return "Burgundy"

What about if you input some wrong keys:

let xml = XML(data: xmlFileData)
// print the error
if let color1 = xml.product.catalog_item.wrong_size.wrong_color.1.xml {
    // do stuff ~
    print(color1)
} else {
    print(xml.product.catalog_item.wrong_size.wrong_color.1.error) //.product.0.catalog_item.0: no such children named: "wrong_size"
}

Requirements

  • iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
  • Xcode 8

Installation

CocoaPods

You can use CocoaPods to install SwiftyXML by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
    pod 'SwiftyXML', '~> 3.0.0'
end

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/SwiftyXML.framework to an iOS project.

github "chenyunguiMilook/SwiftyXML" ~> 3.0.0

Manually

  1. Download and drop XML.swift into your project.
  2. Congratulations!

Swift Package Manager

You can use The Swift Package Manager to install SwiftyXML by adding the proper description to your Package.swift file:

.package(url: "https://github.com/chenyunguiMilook/SwiftyXML.git", from: "3.0.2")

Usage

Initialization

import SwiftyXML
let xml = XML(data: xmlFileData)

Access XML and print out the error

if let color1 = xml.product.catalog_item.wrong_size.wrong_color.1.xml {
    // do stuff ~
    print(color1)
} else {
    print(xml.product.catalog_item.wrong_size.wrong_color.1.error)
}

Catch the error

// catch the error
do {
    let color = try xml.product.catalog_item.wrong_size.wrong_color.1.getXML()
    print(color)
} catch {
    print(error)
}

Access XML List

// handle xml list
for catalog in xml.product.catalog_item {
    for size in catalog.size {
        print(size.$description.stringValue)
    }
}

Read Enums

// read enum value, Notice: enum need implements RawRepresentable
public enum Color : String {
    case Red, Navy, Burgundy
}

if let c: Color = xml.product.catalog_item.size.color_swatch.enum() {
    print(c)
}

Construct XML

let store = XML(name: "store")
    .addAttribute(name: "description", value: "Ball Store")
    .addChildren([
        // attributes can be added in the initializer
        XML(name: "product", attributes: [
            "name": "football",
            "weight": 0.453
        ])
    ])

// attributes can be added to an existing object
let product2 = XML(name: "product")
product2.addAttribute(name: "name", value: "basketball")
product2.addAttribute(name: "weight", value: 0.654)

// children can be added to an existing object
store.addChild(product2)

print(store.toXMLString())
// store xml output
<store description="Ball Store" >
	<product name="football" weight="0.453" />
	<product name="basketball" weight="0.654" />
</store>

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory