Code Monkey home page Code Monkey logo

swiftyxml's Introduction

Carthage compatible

#SwiftyXML

Platform

SwiftyXML use most swifty way to deal with XML data.

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(data: xmlFileData)
let color0 = xml["product"]["catalog_item"]["size"]["color_swatch"][1].string //"Burgundy"
let description0 = xml["product"]["catalog_item"]["size"][1]["@description"].string //"Large"

// or use key chain subscript
let color1 = xml["#product.catalog_item.size.color_swatch.1"].string //"Burgundy"
let description1 = 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 ~
} 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’
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" ~> 1.1.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:

import PackageDescription

let package = Package(
    name: "PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/chenyunguiMilook/SwiftyXML.git", majorVersion: 1, minor: 1)
    ]
)

Usage

####Initialization

import SwiftyXML
let xml = XML(data: xmlFileData)

####Access XML use key chain

// attribute key chain, #: means start key chain subscript, @: means start attribute subscript
xml["#product.catalog_item.size.color_swatch.@image"].stringValue
xml["#product.@description"].stringValue

Access XML and print out the error

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

Catch the error

// catch the error
do {
    let color = try xml["product"]["catalog_item"]["wrong_size"]["wrong_color"][1].getXML()
} 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")
store.addAttribute(name: "description", value: "Ball Store")

let product1 = XML(name: "product")
product1.addAttribute(name: "name", value: "football")
product1.addAttribute(name: "weight", value: 0.453)

let product2 = XML(name: "product")
product2.addAttribute(name: "name", value: "basketball")
product2.addAttribute(name: "weight", value: 0.654)

store.addChild(product1)
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>

swiftyxml's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.