# SWXMLHash
[![CocoaPods](https://img.shields.io/cocoapods/p/SWXMLHash.svg)]()
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods](https://img.shields.io/cocoapods/v/SWXMLHash.svg)](https://cocoapods.org/pods/SWXMLHash)
[![Join the chat at https://gitter.im/drmohundro/SWXMLHash](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/drmohundro/SWXMLHash?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![codebeat](https://codebeat.co/badges/893cc640-c5d9-45b2-a3ff-426e6e6b7b80)](https://codebeat.co/projects/github-com-drmohundro-swxmlhash)
SWXMLHash is a relatively simple way to parse XML in Swift. If you're familiar
with `NSXMLParser`, this library is a simple wrapper around it. Conceptually, it
provides a translation from XML to a dictionary of arrays (aka hash).
The API takes a lot of inspiration from
[SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON).
## Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Configuration](#configuration)
- [Examples](#examples)
- [FAQ](#faq)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)
## Requirements
- iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 8.0+
## Installation
SWXMLHash can be installed using [CocoaPods](http://cocoapods.org/),
[Carthage](https://github.com/Carthage/Carthage),
[Swift Package Manager](https://swift.org/package-manager/), or manually.
### CocoaPods
To install CocoaPods, run:
```bash
$ gem install cocoapods
```
Then create a `Podfile` with the following contents:
```ruby
platform :ios, '10.0'
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'SWXMLHash', '~> 5.0.0'
end
```
Finally, run the following command to install it:
```bash
$ pod install
```
### Carthage
To install Carthage, run (using Homebrew):
```bash
$ brew update
$ brew install carthage
```
Then add the following line to your `Cartfile`:
```
github "drmohundro/SWXMLHash" ~> 5.0
```
### Swift Package Manager
Swift Package Manager requires Swift version 4.0 or higher. First, create a
`Package.swift` file. It should look like:
```swift
dependencies: [
.package(url: "https://github.com/drmohundro/SWXMLHash.git", from: "5.0.0")
]
```
`swift build` should then pull in and compile SWXMLHash for you to begin using.
### Manual Installation
To install manually, you'll need to clone the SWXMLHash repository. You can do
this in a separate directory or you can make use of git submodules - in this
case, git submodules are recommended so that your repository has details about
which commit of SWXMLHash you're using. Once this is done, you can just drop the
`SWXMLHash.swift` file into your project.
> NOTE: if you're targeting iOS 7, you'll have to install manually because
> embedded frameworks require a minimum deployment target of iOS 8 or OSX
> Mavericks.
## Getting Started
If you're just getting started with SWXMLHash, I'd recommend cloning the
repository down and opening the workspace. I've included a Swift playground in
the workspace which makes it easy to experiment with the API and the calls.
<img src="https://raw.githubusercontent.com/drmohundro/SWXMLHash/assets/swift-playground@2x.png" width="600" alt="Swift Playground" />
## Configuration
SWXMLHash allows for limited configuration in terms of its approach to parsing.
To set any of the configuration options, you use the `configure` method, like
so:
```swift
let xml = SWXMLHash.config {
config in
// set any config options here
}.parse(xmlToParse)
```
The available options at this time are:
- `shouldProcessLazily`
- This determines whether not to use lazy loading of the XML. It can
significantly increase the performance of parsing if your XML is large.
- Defaults to `false`
- `shouldProcessNamespaces`
- This setting is forwarded on to the internal `NSXMLParser` instance. It will
return any XML elements without their namespace parts (i.e. "\<h:table\>"
will be returned as "\<table\>")
- Defaults to `false`
- `caseInsensitive`
- This setting allows for key lookups to be case insensitive. Typically XML is
a case sensitive language, but this option lets you bypass this if
necessary.
- Defaults to `false`
- `encoding`
- This setting allows for explicitly specifying the character encoding when an
XML string is passed to `parse`.
- Defaults to `String.encoding.utf8`
- `userInfo`
- This setting mimics `Codable`'s `userInfo` property to allow the user to add
contextual information that will be used for deserialization.
- See
[Codable's userInfo docs](https://developer.apple.com/documentation/swift/encoder/2894907-userinfo)
- The default is [:]
- `detectParsingErrors`
- This setting attempts to detect XML parsing errors. `parse` will return an
`XMLIndexer.parsingError` if any parsing issues are found.
- Defaults to `false` (because of backwards compatibility and because many
users attempt to parse HTML with this library)
## Examples
All examples below can be found in the included
[specs](https://github.com/drmohundro/SWXMLHash/blob/master/Tests/).
### Initialization
```swift
let xml = SWXMLHash.parse(xmlToParse)
```
Alternatively, if you're parsing a large XML file and need the best performance,
you may wish to configure the parsing to be processed lazily. Lazy processing
avoids loading the entire XML document into memory, so it could be preferable
for performance reasons. See the error handling for one caveat regarding lazy
loading.
```swift
let xml = SWXMLHash.config {
config in
config.shouldProcessLazily = true
}.parse(xmlToParse)
```
The above approach uses the new config method, but there is also a `lazy` method
directly off of `SWXMLHash`.
```swift
let xml = SWXMLHash.lazy(xmlToParse)
```
### Single Element Lookup
Given:
```xml
<root>
<header>
<title>Foo</title>
</header>
...
</root>
```
Will return "Foo".
```swift
xml["root"]["header"]["title"].element?.text
```
### Multiple Elements Lookup
Given:
```xml
<root>
...
<catalog>
<book><author>Bob</author></book>
<book><author>John</author></book>
<book><author>Mark</author></book>
</catalog>
...
</root>
```
The below will return "John".
```swift
xml["root"]["catalog"]["book"][1]["author"].element?.text
```
### Attributes Usage
Given:
```xml
<root>
...
<catalog>
<book id="1"><author>Bob</author></book>
<book id="123"><author>John</author></book>
<book id="456"><author>Mark</author></book>
</catalog>
...
</root>
```
The below will return "123".
```swift
xml["root"]["catalog"]["book"][1].element?.attribute(by: "id")?.text
```
Alternatively, you can look up an element with specific attributes. The below
will return "John".
```swift
xml["root"]["catalog"]["book"].withAttribute("id", "123")["author"].element?.text
```
### Returning All Elements At Current Level
Given:
```xml
<root>
...
<catalog>
<book><genre>Fiction</genre></book>
<book><genre>Non-fiction</genre></book>
<book><genre>Technical</genre></book>
</catalog>
...
</root>
```
The `all` method will iterate over all nodes at the indexed level. The code
below will return "Fiction, Non-fiction, Technical".
```swift
", ".join(xml["root"]["catalog"]["book"].all.map { elem in
elem["genre"].element!.text!
})
```
You can also iterate over the `all` method:
```swift
for elem in xml["root"]["catalog"]["book"].all {
print(elem["genre"].element!.text!)
}
```
### Returning All Child Elements At Current Level
Given:
```xml
<root>
<catalog>
<book>
<genre>Fiction</genre>
<title>Book</title>
<date>1/1/2015</date>
</book>
</catalog>
</root>
```
The below will `print` "root", "catalog", "book", "genre", "title", and "date"
(note the `children` method).
没有合适的资源?快使用搜索试试~ 我知道了~
iOS-多媒体-绘制不同的SVG图形-4Macaw
共249个文件
swift:148个
plist:11个
xcconfig:4个
需积分: 5 0 下载量 161 浏览量
2023-05-20
19:43:07
上传
评论
收藏 759KB ZIP 举报
温馨提示
iOS_多媒体_绘制不同的SVG图形_4Macaw
资源推荐
资源详情
资源评论
收起资源包目录
iOS-多媒体-绘制不同的SVG图形-4Macaw (249个子文件)
08767942efb50b50b9a3c79288121cd70a2a9f 58B
13406b586b893f26451af665cae83887231633 249B
13adab9f4eab78fad8e1cf293c1586417e98a7 1KB
19b061fec8bc245518acee6aaa2d4be36419f1 84B
1f9f972af2ebade364efafdbd370c7ac3de23b 133B
2673ba6c01ab1e6d3246ae794a2f6a57e9d92e 1KB
2e5a135d52a961322b08ce39810b3a71ac88b2 68B
2fe05df2d5b8dd25124190b4ffb537c344d5c8 248B
3348d87704fa6b151e425325bd52055b823c47 101B
3cc9b8ba4e4ae5c6df5ed5e3f199668b7fead0 68B
46e2df5c16a9a315bebadb2ea39f30febb2880 3KB
4a164c918651cdd1e11dca5cc62c333f097601 65B
59fac26da4ba4532e016844888b8a884770a86 148B
5a4d6031d6f8c18b72dfccff19d8f7b4d5cad0 59B
601a348d3b63b433a845d494790b9fc32d7df8 936B
6d8935f615f36cbdd5c5fe653228769118504c 255B
7e86c2a1454021f1cce792169ba5aeb0720689 95B
838307fc9540a37af7b6eb6e74dbcc29a2a02a 160KB
8e91aa34f2bd4dc559d9fd235516d63ea91edf 51B
8fd593c441981dff6a08eabbc22e083a70a5ba 677B
9120a84259f3fbf9acce40672c35cab389dca5 1KB
99fe48a454e1f2009dd0d21a0dc1de46a4c543 133B
a36129419f8bd7ad73581cb9f07b8c6eec3fcf 792B
a4ce6a06ffe79452c5a9fd5f6ca47cd6e7b9a1 105B
a5f7144cdefdff3cd41b869a4ae0fe8b00225f 207B
b0b42ec057f4a739bb34e31ea9c8592f3bd3f4 146B
bb4c5451f2a810965e83d031e2a396cd651502 312B
bcf38400d14f257132ac2ae8c09f25b844ba1c 758B
be3b681122de83e380d47b840b7d0486f71f86 540B
cdeb8eafe518cb375f43602206d29819687cf0 35KB
COMMIT_EDITMSG 15B
config 137B
d7dad2b232229a00affd19e36eb4fe2f430243 248B
d981003d68d0546c4804ac2ff47dd97c6e7921 206B
db8d65fd79fd541b2b7eba75c7378af3448f9c 254B
description 73B
.DS_Store 6KB
.DS_Store 6KB
exclude 40B
Pods-DemoApp-umbrella.h 316B
SWXMLHash-umbrella.h 310B
Macaw-umbrella.h 302B
HEAD 171B
HEAD 23B
index 2KB
Contents.json 2KB
Contents.json 123B
Contents.json 63B
LICENSE 1KB
LICENSE 1KB
Manifest.lock 370B
Podfile.lock 370B
Pods-DemoApp-dummy.m 128B
SWXMLHash-dummy.m 122B
Macaw-dummy.m 114B
Pods-DemoApp-acknowledgements.markdown 2KB
master 171B
master 41B
README.md 17KB
README.md 5KB
Pods-DemoApp.modulemap 114B
SWXMLHash.modulemap 108B
Macaw.modulemap 100B
project.pbxproj 109KB
project.pbxproj 19KB
SWXMLHash-prefix.pch 195B
Macaw-prefix.pch 195B
Pods-DemoApp-acknowledgements.plist 3KB
Info.plist 2KB
Pods-DemoApp-Info.plist 828B
SWXMLHash-Info.plist 828B
Macaw-Info.plist 828B
xcschememanagement.plist 682B
xcschememanagement.plist 565B
xcschememanagement.plist 342B
xcschememanagement.plist 342B
IDEWorkspaceChecks.plist 238B
IDEWorkspaceChecks.plist 238B
coffee.png 174KB
Podfile 129B
README.sample 177B
Pods-DemoApp-frameworks.sh 8KB
Main.storyboard 3KB
LaunchScreen.storyboard 2KB
tiger.svg 94KB
SVGParser.swift 81KB
SWXMLHash.swift 34KB
XMLIndexer+XMLIndexerDeserializable.swift 29KB
MacawView.swift 24KB
RenderUtils.swift 23KB
SVGSerializer.swift 15KB
NodeRenderer.swift 13KB
ShapeRenderer.swift 11KB
SVGConstants.swift 10KB
AnimationProducer.swift 10KB
Node.swift 10KB
Color.swift 8KB
AnimationCache.swift 8KB
AnimationImpl.swift 7KB
ShapeAnimationGenerator.swift 7KB
共 249 条
- 1
- 2
- 3
资源评论
GotoLevel
- 粉丝: 23
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功