Fuzi (斧子)
A fast & lightweight XML/HTML parser in Swift that makes your life easier. [Documentation]
Fuzi is based on a Swift port of Mattt Thompson's Ono(斧), using most of its low level implementaions with moderate class & interface redesign following standard Swift conventions, along with several bug fixes.
Fuzi(斧子) means "axe", in homage to Ono(斧), which in turn is inspired by Nokogiri (鋸), which means "saw".
A Quick Look
Features
Inherited from Ono
- Extremely performant document parsing and traversal, powered by
libxml2
- Support for both XPath and CSS queries
- Automatic conversion of date and number values
- Correct, common-sense handling of XML namespaces for elements and attributes
- Ability to load HTML and XML documents from either
String
orNSData
or[CChar]
- Comprehensive test suite
- Full documentation
Improved in Fuzi
- Simple, modern API following standard Swift conventions, no more return types like
AnyObject!
that cause unnecessary type casts - Customizable date and number formatters
- Some bugs fixes
- More convenience methods for HTML Documents
- Access XML nodes of all types (Including text, comment, etc.)
- Support for more CSS selectors (yet to come)
Requirements
- iOS 8.0+ / Mac OS X 10.9+
- Xcode 8.0+
Use version 0.4.0 for Swift 2.3.
Installation
There are 4 ways you can install Fuzi to your project.
Using CocoaPods
You can use CocoaPods to install Fuzi
by adding it to your to your Podfile
:
Then, run the following command:
Using Swift Package Manager
The Swift Package Manager is now built-in with Xcode 11 (currently in beta). You can easily add Fuzi as a dependency by choosing File > Swift Packages > Add Package Dependency...
or in the Swift Packages tab of your project file and clicking on +
.
Simply use https://github.com/cezheng/Fuzi
as repository and Xcode should automatically resolve the current version.
Manually
- Add all
*.swift
files inFuzi
directory into your project. - In your Xcode project
Build Settings
:- Find
Search Paths
, add$(SDKROOT)/usr/include/libxml2
toHeader Search Paths
. - Find
Linking
, add-lxml2
toOther Linker Flags
.
- Find
Using Carthage
Create a Cartfile
or Cartfile.private
in the root directory of your project, and add the following line:
github "cezheng/Fuzi" ~> 1.0.0
Run the following command:
$ carthage update
Then do the followings in Xcode:
- Drag the
Fuzi.framework
built by Carthage into your target'sGeneral
->Embedded Binaries
. - In
Build Settings
, findSearch Paths
, add$(SDKROOT)/usr/include/libxml2
toHeader Search Paths
.
Usage
XML
HTML
HTMLDocument
is a subclass of XMLDocument
.
I don't care about error handling
I want to access Text Nodes
Not only text nodes, you can specify what types of nodes you would like to access.
Migrating From Ono?
Looking at example programs is the swiftest way to know the difference. The following 2 examples do exactly the same thing.
Accessing children
Ono
Fuzi
Iterate through query results
Ono
Conforms to NSFastEnumeration
.
Fuzi
Conforms to Swift's SequenceType
and Indexable
.
Evaluating XPath Functions
Ono
Fuzi
License
Fuzi
is released under the MIT license. See LICENSE for details.