SwiftyXML 3.1.0

SwiftyXML 3.1.0

测试已测试
语言 SwiftSwift
许可 MIT
发布上次发布2020年10月
SPM支持 SPM

chenyunguiMilook 维护。



SwiftyXML 3.1.0

  • chenyunguiMilook

Carthage compatible

SwiftyXML

Platform

SwiftyXML 使用最Swift的方式来处理XML数据。

特性

  • 无限下标
  • 动态成员查找支持(使用以 $ 开头的字符串作为属性的下标访问)
  • 可选或非可选值的访问
  • 直接访问枚举类型值(枚举扩展自 RawRepresentable)
  • 在 XML 子节点中直接进行 for 循环
  • 精确的错误抛出
  • XML 结构,格式化
  • 单源文件

样例 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>

使用 SwiftyXML,您只需做这样:

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"

这等同于下面的,SwiftyXML 会自动选择第一个元素作为默认元素

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

如果您输入了一些错误的键呢?

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"
}

要求

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

安装

CocoaPods

您可以使用 CocoaPods 来安装 SwiftyXML,方法是将它添加到您的 Podfile 文件中。

platform :ios, '8.0'
use_frameworks!

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

Carthage

创建一个列出框架的 Cartfile 文件,然后运行 carthage update。按照说明添加 $(SRCROOT)/Carthage/Build/iOS/SwiftyXML.framework 到 iOS 项目中。

github "chenyunguiMilook/SwiftyXML" ~> 3.0.0

手动

  1. 下载并将 XML.swift 拖放到您的项目中。
  2. 恭喜您!

Swift 包管理器

您可以使用Swift 包管理器 通过将适当的描述添加到您的 Package.swift 文件中来安装 SwiftyXML

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

使用

初始化

import SwiftyXML
let xml = XML(data: xmlFileData)

访问XML并打印错误

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
do {
    let color = try xml.product.catalog_item.wrong_size.wrong_color.1.getXML()
    print(color)
} catch {
    print(error)
}

访问XML列表

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

读取枚举

// 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)
}

构建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>