Fast DOM parser & serializer in pure Swift for iOS & Mac
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)")
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>
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
- Open a Terminal window
- Change to your projects directory
cd /path/to/MyProject
- If this is a new project, initialize Git:
git init
- Add the submodule:
git submodule add https://github.com/glimpseio/GlimpseXML.git GlimpseXML
.
Set up Xcode
- Find the
GlimpseXML.xcodeproj
file inside of the cloned GlimpseXML project directory. - Drag & Drop it into the
Project Navigator
(⌘+1). - Select your project in the
Project Navigator
(⌘+1). - Select your target.
- Select the tab
Build Phases
. - Expand
Link Binary With Libraries
. - Add
GlimpseXML.framework
- Add
import GlimpseXML
to the top of your Swift source files.