Giter VIP home page Giter VIP logo

glimpsexml's Introduction

GlimpseXML

Version Build Status

Fast DOM parser & serializer in pure Swift for iOS & Mac

How to get started

First, install GlimpseXML via CocoaPods or manually.

CocoaPods

platform :ios, '8.0'
use_frameworks! # For Swift projects
  
target 'SampleTarget' do
  pod 'GlimpseXML' 
end

Lib Usage Examples

Parsing & XPath Example

import GlimpseXML

let music = "~/Music/iTunes/iTunes Music Library.xml".stringByExpandingTildeInPath
let doc = try GlimpseXML.Document.parseFile(music)
let rootNodeName: String? = doc.rootElement.name
println("Library Type: \(rootNodeName)")

let trackCount = doc.xpath("/plist/dict/key[text()='Tracks']/following-sibling::dict/key").value?.first?.text
println("Track Count: \(trackCount)")

let dq = doc.xpath("//key[text()='Artist']/following-sibling::string[text()='Bob Dylan']").value?.count
println("Dylan Quotient: \(dq)")

Generating & Serializing Example

You can manually create an XML DOM using Glimpse.XML Node elements. Convenience constructors are provided in order to make tree construction naturally fit the hierarchy of an XML document.

import GlimpseXML

let node = Node(name: "library", attributes: [("url", "glimpse.io")], children: [
    Node(name: "inventory", children: [
        Node(name: "book", attributes: [("checkout", "true")], children: [
            Node(name: "title", text: "I am a Bunny" ),
            Node(name: "author", text: "Richard Scarry"),
            ]),
        Node(name: "book", attributes: [("checkout", "false")], children: [
            Node(name: "title", text: "You were a Bunny" ),
            Node(name: "author", text: "Scarry Richard"),
            ]),
        ]),
    ])

You can also serialize the node to a String:

let compact: String = node.serialize()
println(compact)

Yielding:

<library url="glimpse.io"><inventory><book checkout="true"><title>I am a Bunny</title><author>Richard Scarry</author></book><book checkout="false"><title>You were a Bunny</title><author>Scarry Richard</author></book></inventory></library>

With formatting:

let formatted: String = node.serialize(indent: true)
println(formatted)
<library url="glimpse.io">
  <inventory>
    <book checkout="true">
      <title>I am a Bunny</title>
      <author>Richard Scarry</author>
    </book>
    <book checkout="false">
      <title>You were a Bunny</title>
      <author>Scarry Richard</author>
    </book>
  </inventory>
</library>

You can also include a doc header with an encoding by wrapping the Node in a Document:

let doc = Document(root: node)
let encoded: String = doc.serialize(indent: true, encoding: "ISO-8859-1")
println(encoded)

Which will output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<library url="glimpse.io">
  <inventory>
    <book checkout="true">
      <title>I am a Bunny</title>
      <author>Richard Scarry</author>
    </book>
    <book checkout="false">
      <title>You were a Bunny</title>
      <author>Scarry Richard</author>
    </book>
  </inventory>
</library>

Setting up GlimpseXML

Cocoapod

pod 'GlimpseXML'

Manual

GlimpseXML is a single cross-platform iOS & Mac Framework. To set it up in your project, simply add it as a github submodule, drag the GlimpseXML.xcodeproj into your own project file, add GlimpseXML.framework to your target's dependencies, and import GlimpseXML from any Swift file that should use it.

Set up Git submodule

  1. Open a Terminal window
  2. Change to your projects directory cd /path/to/MyProject
  3. If this is a new project, initialize Git: git init
  4. Add the submodule: git submodule add https://github.com/glimpseio/GlimpseXML.git GlimpseXML.

Set up Xcode

  1. Find the GlimpseXML.xcodeproj file inside of the cloned GlimpseXML project directory.
  2. Drag & Drop it into the Project Navigator (โŒ˜+1).
  3. Select your project in the Project Navigator (โŒ˜+1).
  4. Select your target.
  5. Select the tab Build Phases.
  6. Expand Link Binary With Libraries.
  7. Add GlimpseXML.framework
  8. Add import GlimpseXML to the top of your Swift source files.

glimpsexml's People

Contributors

joemcbride avatar marcprux avatar pauliusvindzigelskis avatar venj avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

carabina

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.