I\'m trying to download data in XML format via a REST web services. I have already created the parser with NSXMLParser and visualize the data in a TableView.
I have
The parser:foundCharacters:
delegate method can be called more than
once for a single XML element. In your example, it would be called
once with "Contabilit", and once with "à nazionale".
Therefore you have to concatenate
the found strings from didStartElement
until didEndElement
.
Here is a very simply example how this can be done. Of course it gets more complicated if you have nested XML elements.
Add a property for the current element string to your class:
var currentElement : String?
And then implement the delegate methods like this:
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
// If a "Name" element started (which you are interested in), set
// currentElement to an empty string, so that the found characters
// can be collected. Otherwise set it to nil.
if elementName == "Name" {
currentElement = ""
} else {
currentElement = nil
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
// If the "Name" element ended, get the collected string and
// append it to your list.
if elementName == "Name" {
if let name = currentElement {
println(name)
myList.append(name)
}
}
currentElement = nil
}
func parser(parser: NSXMLParser, foundCharacters string: String?) {
// If currentElement is not nil, append the found characters to it:
currentElement? += string ?? ""
}