PauliusVindzigelskis / GlimpseXML

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

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

License:MIT License


Languages

Language:Swift 92.7%Language:Ruby 4.3%Language:Objective-C 1.3%Language:Shell 1.2%Language:C 0.4%