0

I want to convert an XML string into a tuple.

var responseXml =
"""
<?xml version="1.0"?>
<tmweb>
<booking type='come' time='71102' persnr='9999' name='Test' firstname='Max' title='Mr.'/>
</tmweb>
"""

responseXml.removeFirst(39)                                          // Remove the beginning of the XML
responseXml.removeLast(11)                                           // Remove the end of the XML
responseXml = responseXml.replacingOccurrences(of:" ", with: ";")   // Replace empty place with ;
responseXml = responseXml.replacingOccurrences(of: "'", with: "\"")    // Replace ' to "
responseXml = responseXml.replacingOccurrences(of: "=", with: ": ")    // Replace = to :(space)

Temporary output:

"type: "come";time: "71102";persnr: "9999";name: "Test";firstname: "Max";title: "Mr."\n"

At the moment I only have the whole string as with the replacements as a UIAlert

My next steps:

I want to convert the seconds (71102) to readable time format like 19:45:22

func secs2time (_ seconds : Int) -> (Int,Int,Int) {
    return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

At the moment it's hard to find a good solution.

Any suggestions?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

To handle the xml you will need to user NSXMLParser. Here are two sites that can help you.

http://leaks.wanari.com/2016/08/24/xml-parsing-swift/ https://medium.com/@lucascerro/understanding-nsxmlparser-in-swift-xcode-6-3-1-7c96ff6c65bc

Here is a little example:

var url = NSURL(string: "http://example.com/website-with-xml")
var xmlParser = NSXMLParser(contentsOfURL: url)
xmlParser.delegate = self
xmlParser.parse()

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) {
   println("Element's name is \(elementName)")
   println("Element's attributes are \(attributeDict)")
}

For the time conversion you can found it on other question here in Stackoverflow:

Define

func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
  return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

Use

secondsToHoursMinutesSeconds(27005) (7,30,5)

or

let (h,m,s) = secondsToHoursMinutesSeconds(27005)

The above function makes use of Swift tuples to return three values at once. You destructure the tuple using the let (var, ...) syntax or can access individual tuple members, if need be.

If you actually need to print it out with the words Hours etc then use something like this:

func printSecondsToHoursMinutesSeconds (seconds:Int) -> () {
  let (h, m, s) = secondsToHoursMinutesSeconds (seconds)
  print ("\(h) Hours, \(m) Minutes, \(s) Seconds")
}

Note that the above implementation of secondsToHoursMinutesSeconds() works for Int arguments. If you want a Double version you'll need to decide what the return values are - could be (Int, Int, Double) or could be (Double, Double, Double). You could try something like:

func secondsToHoursMinutesSeconds (seconds : Double) -> (Double, Double, Double) {
  let (hr,  minf) = modf (seconds / 3600)
  let (min, secf) = modf (60 * minf)
  return (hr, min, 60 * secf)
}
GuiDupas
  • 1,681
  • 2
  • 22
  • 44