- Notifications
You must be signed in to change notification settings - Fork196
A sensible way to deal with XML & HTML for iOS & macOS
License
mattt/Ono
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Foundation lacks a convenient, cross-platform way to work with HTML and XML.NSXMLParser
is an event-driven,SAX-style APIthat can be cumbersome to work with.NSXMLDocument
,offers a more convenientDOM-style API,but is only supported on macOS.
Ono offers a sensible way to work with XML & HTML on Apple platforms in Objective-C and Swift
Whether your app needs toscrape a website, parse an RSS feed, or interface with a XML-RPC webservice,Ono will make your day a whole lot less terrible.
Ono (斧) means "axe", in homage toNokogiri (鋸), which means "saw".
- Simple, modern API following standard Objective-C conventions, including extensive use of blocks and
NSFastEnumeration
- Extremely performant document parsing and traversal, powered by
libxml2
- Support for bothXPath andCSS 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
NSString
orNSData
- Full documentation
- Comprehensive test suite
CocoaPods is the recommended method of installing Ono.Add the following line to yourPodfile
:
pod'Ono'
import Foundationimport Onoguardlet url=Bundle.main.url(forResource:"nutrition", withExtension:"xml"),let data=try?Data(contentsOf: url)else{fatalError("Missing resource: nutrition.xml")}letdocument=tryONOXMLDocument(data: data)document.rootElement.tagforelementin document.rootElement.children.first?.children??[]{letnutrient= element.tagletamount= element.numberValue!letunit= element.attributes["units"]!print("-\(amount)\(unit)\(nutrient)")}document.enumerateElements(withXPath:"//food/name"){(element, _, _)inprint(element)}document.enumerateElements(withCSS:"food > serving[units]"){(element, _, _)inprint(element)}
#import"Ono.h"NSData *data = ...;NSError *error;ONOXMLDocument *document = [ONOXMLDocumentXMLDocumentWithData:dataerror:&error];for (ONOXMLElement *element in document.rootElement.children) {NSLog(@"%@:%@", element.tag, element.attributes);}// Support for NamespacesNSString *author = [[document.rootElementfirstChildWithTag:@"creator"inNamespace:@"dc"]stringValue];// Automatic Conversion for Number & Date ValuesNSDate *date = [[document.rootElementfirstChildWithTag:@"created_at"]dateValue];// ISO 8601 TimestampNSInteger numberOfWords = [[[document.rootElementfirstChildWithTag:@"word_count"]numberValue]integerValue];BOOL isPublished = [[[document.rootElementfirstChildWithTag:@"is_published"]numberValue]boolValue];// Convenient Accessors for AttributesNSString *unit = [document.rootElementfirstChildWithTag:@"Length"][@"unit"];NSDictionary *authorAttributes = [[document.rootElementfirstChildWithTag:@"author"]attributes];// Support for XPath & CSS Queries[documentenumerateElementsWithXPath:@"//Content"usingBlock:^(ONOXMLElement *element,NSUInteger idx,BOOL *stop) {NSLog(@"%@", element);}];
Build and run the example project in Xcode to seeOno
in action,or check out the provided Swift Playground.
Ono is compatible with iOS 5 and higher, as well as macOS 10.7 and higher.It requires thelibxml2
library,which is included automatically when installed with CocoaPods,or added manually by adding "libxml2.dylib"to the target's "Link Binary With Libraries" build phase.
Ono is available under the MIT license.See the LICENSE file for more info.
About
A sensible way to deal with XML & HTML for iOS & macOS