Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/OnoPublic

A sensible way to deal with XML & HTML for iOS & macOS

License

NotificationsYou must be signed in to change notification settings

mattt/Ono

Repository files navigation

Foundation lacks a convenient, cross-platform way to work with HTML and XML.NSXMLParseris 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".

Features

  • Simple, modern API following standard Objective-C conventions, including extensive use of blocks andNSFastEnumeration
  • Extremely performant document parsing and traversal, powered bylibxml2
  • 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 eitherNSString orNSData
  • Full documentation
  • Comprehensive test suite

Installation

CocoaPods is the recommended method of installing Ono.Add the following line to yourPodfile:

Podfile

pod'Ono'

Usage

Swift

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

Objective-C

#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);}];

Demo

Build and run the example project in Xcode to seeOno in action,or check out the provided Swift Playground.

Requirements

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.

Contact

Mattt

License

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp