Skip to content

Instantly share code, notes, and snippets.

@drmohundro
Created November 26, 2014 04:23
Show Gist options
  • Save drmohundro/62d83c3910d28e49fb17 to your computer and use it in GitHub Desktop.
Save drmohundro/62d83c3910d28e49fb17 to your computer and use it in GitHub Desktop.
SWXMLHash parsing XML with namespace
// Playground - noun: a place where people can play
import SWXMLHash
import UIKit
let xmlWithNamespace = "<root xmlns:h=\"http://www.w3.org/TR/html4/\"" +
" xmlns:f=\"http://www.w3schools.com/furniture\">" +
" <h:table>" +
" <h:tr>" +
" <h:td>Apples</h:td>" +
" <h:td>Bananas</h:td>" +
" </h:tr>" +
" </h:table>" +
" <f:table>" +
" <f:name>African Coffee Table</f:name>" +
" <f:width>80</f:width>" +
" <f:length>120</f:length>" +
" </f:table>" +
"</root>"
var xml = SWXMLHash.parse(xmlWithNamespace)
// one root element
let count = xml["root"].all.count
// "Apples"
xml["root"]["h:table"]["h:tr"]["h:td"][0].element!.text!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment