SWXMLHash Docs (93% documented)

SWXMLHash Reference SWXMLHash Reference

SWXMLHash

SWXMLHash

SWXMLHash is a relatively simple way to parse XML in Swift. If you’re familiar with NSXMLParser, this library is a simple wrapper around it. Conceptually, it provides a translation from XML to a dictionary of arrays (aka hash).

The API takes a lot of inspiration from SwiftyJSON.

Installation

Ultimately, this will be distributed with CocoaPods support; however, CocoaPods doesn’t yet support Swift projects. In the meantime, just add SWXMLHash as a git submodule and drag SWXMLHash.swift into your project.

Getting Started

If you’re just getting started with SWXMLHash, I’d recommend cloning the repository down and opening the workspace. I’ve included a Swift playground in the workspace which makes it very easy to experiment with the API and the calls.

Swift Playground

Examples

All examples below can be found in the included specs.

Initialization

let xml = SWXMLHash.parse(xmlToParse)

Single Element Lookup

Given:

<root>
  <header>
    <title>Foo</title>
  </header>
  ...
</root>

Will return Foo.

xml["root"]["header"]["title"].element?.text

Multiple Elements Lookup

Given:

<root>
  ...
  <catalog>
    <book><author>Bob</author></book>
    <book><author>John</author></book>
    <book><author>Mark</author></book>
  </catalog>
  ...
</root>

The below will return John.

xml["root"]["catalog"]["book"][1]["author"].element?.text

Attributes Usage

Given:

<root>
  ...
  <catalog>
    <book id="1"><author>Bob</author></book>
    <book id="123"><author>John</author></book>
    <book id="456"><author>Mark</author></book>
  </catalog>
  ...
</root>

The below will return 123.

xml["root"]["catalog"]["book"][1].element?.attributes["id"]

Alternatively, you can look up an element with specific attributes. The below will return John.

xml["root"]["catalog"]["book"].withAttr("id", "123")["author"].element?.text

Returning All Elements At Current Level

Given:

<root>
  ...
  <catalog>
    <book><genre>Fiction</genre></book>
    <book><genre>Non-fiction</genre></book>
    <book><genre>Technical</genre></book>
  </catalog>
  ...
</root>

The below will return Fiction, Non-fiction, Technical (note the all method).

", ".join(xml["root"]["catalog"]["book"].all.map { elem in
  elem["genre"].element!.text!
})

Alternatively, you can just iterate over the elements using for-in directly against an element.

for elem in xml["root"]["catalog"]["book"] {
  NSLog(elem["genre"].element!.text!)
}

Returning All Child Elements At Current Level

Given:

<root>
  <catalog>
    <book>
      <genre>Fiction</genre>
      <title>Book</title>
      <date>1/1/2015</date>
    </book>
  </catalog>
</root>

The below will NSLog root, catalog, book, genre, title, and date (note the children method).

func enumerate(indexer: XMLIndexer) {
  for child in indexer.children {
    NSLog(child.element!.name)
    enumerate(child)
  }
}

enumerate(xml)

Error Handling

switch xml["root"]["what"]["header"]["foo"] {
case .Element(let elem):
  // everything is good, code away!
case .Error(let error):
  // error is an NSError instance that you can deal with
}

TODO

  • [x] finish implementing error handling for group indexing
  • [x] add attribute support
  • [x] maybe add attribute look-up for elements as opposed to solely array indexing
  • [ ] add CocoaPods support once it supports Swift projects
  • [ ] more???

License

SWXMLHash is released under the MIT license. See LICENSE for details.