GithubHelp home page GithubHelp logo

jay18001 / ono Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattt/ono

0.0 1.0 2.0 328 KB

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

License: MIT License

Objective-C 96.88% Ruby 0.84% Swift 2.28%

ono's Introduction

Ono (斧)

Foundation lacks a convenient, cross-platform way to work with HTML and XML. NSXMLParser is an event-driven, SAX-style API that can be cumbersome to work with. NSXMLDocument, offers a more convenient DOM-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 to scrape 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 to Nokogiri (鋸), which means "saw".

Features

  • 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 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 NSString or NSData
  • Full documentation
  • Comprehensive test suite

Installation

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

Podfile

pod 'Ono'

Usage

Swift

import Foundation
import Ono

guard let url = Bundle.main.url(forResource: "nutrition", withExtension: "xml"),
    let data = try? Data(contentsOf: url) else
{
    fatalError("Missing resource: nutrition.xml")
}

let document = try ONOXMLDocument(data: data)
document.rootElement.tag

for element in document.rootElement.children.first?.children ?? [] {
    let nutrient = element.tag
    let amount = element.numberValue!
    let unit = element.attributes["units"]!

    print("- \(amount)\(unit) \(nutrient)")
}

document.enumerateElements(withXPath: "//food/name") { (element, _, _) in
    print(element)
}

document.enumerateElements(withCSS: "food > serving[units]") { (element, _, _) in
    print(element)
}

Objective-C

#import "Ono.h"

NSData *data = ...;
NSError *error;

ONOXMLDocument *document = [ONOXMLDocument XMLDocumentWithData:data error:&error];
for (ONOXMLElement *element in document.rootElement.children) {
    NSLog(@"%@: %@", element.tag, element.attributes);
}

// Support for Namespaces
NSString *author = [[document.rootElement firstChildWithTag:@"creator" inNamespace:@"dc"] stringValue];

// Automatic Conversion for Number & Date Values
NSDate *date = [[document.rootElement firstChildWithTag:@"created_at"] dateValue]; // ISO 8601 Timestamp
NSInteger numberOfWords = [[[document.rootElement firstChildWithTag:@"word_count"] numberValue] integerValue];
BOOL isPublished = [[[document.rootElement firstChildWithTag:@"is_published"] numberValue] boolValue];

// Convenient Accessors for Attributes
NSString *unit = [document.rootElement firstChildWithTag:@"Length"][@"unit"];
NSDictionary *authorAttributes = [[document.rootElement firstChildWithTag:@"author"] attributes];

// Support for XPath & CSS Queries
[document enumerateElementsWithXPath:@"//Content" usingBlock:^(ONOXMLElement *element, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@", element);
}];

Demo

Build and run the example project in Xcode to see Ono 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 the libxml2 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.

ono's People

Contributors

cezheng avatar choefele avatar cxa avatar davidchiles avatar dcaunt avatar dtorres avatar dulacp avatar erikprice avatar grantjbutler avatar hactar avatar indragiek avatar jay18001 avatar jeehut avatar joshuatbrown avatar justadreamer avatar kr1sso avatar mattt avatar nhart12 avatar palleas avatar rhodgkins avatar royalicing avatar slightair avatar squarefrog avatar sspitzer avatar wenchaod avatar wojtkow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.