GithubHelp home page GithubHelp logo

kaakati / ji Goto Github PK

View Code? Open in Web Editor NEW

This project forked from honghaoz/ji

0.0 2.0 0.0 737 KB

Ji (戟) is an XML/HTML parser for Swift

License: MIT License

Swift 94.00% Objective-C 3.86% Ruby 2.13%

ji's Introduction

Ji: a Swift XML/HTML parser

Ji 戟

CI Status Cocoapods Version License Platform

Ji (戟) is a Swift wrapper on libxml2 for parsing XML/HTML.

Features

  • Build XML/HTML Tree and Navigate
  • XPath Query Supported
  • Comprehensive Unit Test Coverage

Requirements

  • iOS 8.0+ / Mac OS X 10.9+
  • Xcode 7.0

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

To integrate Ji into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'Ji', '~> 1.1.2'

Then, run the following command:

$ pod install

Carthage

To integrate Ji into your Xcode project using Carthage, specify it in your Cartfile:

github "honghaoz/Ji" >= 1.1.2

Manually

If you prefer not to use a dependency manager, you can integrate Ji into your project manually.

  • Configure Xcode project:
    • Open project, select the target, under General, in Linked Frameworks and Libraries, add libxml2.2.dylib and libxml2.dylib
    • Under Build Settings, in Header Search Paths, add $(SDKROOT)/usr/include/libxml2
    • Under Build Settings, in Other Linker Flags, add -lxml2
  • Import libxml headers:
    • Copy the those import statements:
    #import <libxml/tree.h>
    #import <libxml/parser.h>
    #import <libxml/HTMLtree.h>
    #import <libxml/HTMLparser.h>
    #import <libxml/xpath.h>
    #import <libxml/xpathInternals.h>
    #import <libxml/xmlerror.h>
    and paste them into your [Modulename]-Bridging-Header.h
  • Drag Ji.swift, JiHelper.swift and JiNode.swift in Source folder into your project.

And that's it!

Usage

If you are using Cocoapods to integrate Ji. Import Ji first:

import Ji
  • Init with NSURL:
let jiDoc = Ji(htmlURL: NSURL(string: "http://www.apple.com/support")!)
let titleNode = jiDoc?.xPath("//head/title")?.first
println("title: \(titleNode?.content)") // title: Optional("Official Apple Support")
  • Init with String:
let xmlString = "<?xml version='1.0' encoding='UTF-8'?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
let jiDoc = Ji(xmlString: xmlString)
let bodyNode = jiDoc?.rootNode?.firstChildWithName("body")
println("body: \(bodyNode?.content)") // body: Optional("Don\'t forget me this weekend!")
  • Init with NSData:
let googleIndexData = NSData(contentsOfURL: NSURL(string: "http://www.google.com")!)
if let googleIndexData = googleIndexData {
	let jiDoc = Ji(htmlData: googleIndexData)!
	let htmlNode = jiDoc.rootNode!
	println("html tagName: \(htmlNode.tagName)") // html tagName: Optional("html")
	
	let aNodes = jiDoc.xPath("//body//a")
	if let firstANode = aNodes?.first {
		println("first a node tagName: \(firstANode.name)") // first a node tagName: Optional("a")
		let href = firstANode["href"]
		println("first a node href: \(href)") // first a node href: Optional("http://www.google.ca/imghp?hl=en&tab=wi")
	}
} else {
	println("google.com is inaccessible")
}

let 戟文档 = (htmlURL: NSURL(string: "https://cocoapods.org/pods/Ji")!)
let attribution = 戟文档?.xPath("//ul[@class='attribution']")?.first
println("作者(Author): \(attribution?.content)")

License

The MIT License (MIT)

Copyright (c) 2015 Honghao Zhang (张宏昊)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ji's People

Contributors

honghaoz avatar ujell avatar

Watchers

 avatar  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.