Swift parse html table -
i trying bring info web page ios app.
html code:
<table border="0" cellpadding="3" cellspacing="0" width="85%"><tr><td width="100%" colspan="3" bgcolor="#c9c9e7"><b>update of 3:57:00 pm (cdt) thu., apr. 16, 2015</b><br></td></tr><tr> <td width="50%" bgcolor="#ffffff">production line 1</td> <td width="35%" bgcolor="#ffffff">9:00 minutes (10 min) </td> <td width="15%" bgcolor="#ffffff">no delay</td> </tr><tr> <td width="50%" bgcolor="#ffffff"><b>production line 2</b></td> <td width="35%" bgcolor="#ffffff"><b>7:57 minutes </b><b>(4 min) </b></td> <td width="15%" bgcolor="#ffffff"><b>+3:57</b></td> </tr><tr> <td width="50%" bgcolor="#ffffff"><b>production line 3</b></td> <td width="35%" bgcolor="#ffffff"><b>10:35 minutes </b><b>(8 min) </b></td> <td width="15%" bgcolor="#ffffff"><b>+2:35</b></td> </tr></table>
this current attempt @ code. have marked line think issue // issue. optional nil values. using swift-html-parser make happen. made 1 change , add section table.
import uikit extension string { var html2string:string { return nsattributedstring(data: datausingencoding(nsutf8stringencoding)!, options: [nsdocumenttypedocumentattribute:nshtmltextdocumenttype,nscharacterencodingdocumentattribute:nsutf8stringencoding], documentattributes: nil, error: nil)!.string } } class mainviewcontroller: uiviewcontroller { @iboutlet var textview: uitextview! override func viewdidload() { super.viewdidload() updatetrafficinfo() let singlefingertap = uitapgesturerecognizer(target: self, action: "handlesingletap:") self.textview.addgesturerecognizer(singlefingertap) } // mark: gestutre recognizer func handlesingletap(recognizer: uitapgesturerecognizer) { updatetrafficinfo() } func updatetrafficinfo(){ var request = httptask() var err: nserror? request.get("http://localhost/productiontimes.html", parameters: nil, success: {(response: httpresponse) in if let data = response.responseobject as? nsdata { let rawhtml = nsstring(data: data, encoding: nsutf8stringencoding) as! string let option = cint(html_parse_noerror.value | html_parse_recover.value) var parser = htmlparser(html: rawhtml, encoding: nsutf8stringencoding, option: option, error: &err) if err != nil { println(err) } var bodynode = parser.table if let inputnodes = bodynode?.xpath("//tr") { node in inputnodes { dispatch_sync(dispatch_get_main_queue(), { self.textview.text = node.xpath("//td")?[0].contents //issue println(node.xpath("//td")?[0].contents) }); } } dispatch_sync(dispatch_get_main_queue(), { self.textview.text = rawhtml.html2string }); } },failure: {(error: nserror, response: httpresponse?) in println("error: \(error)") }) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
goal have output in format of this.
production line - time - delay production line 1 - 9:00 minutes (10 min) - no delay production line 2 - 7:57 minutes (4 min) - +3:57
extension nsdata { var htmldata2string:string { return nsattributedstring(data: self, options: [nsdocumenttypedocumentattribute : nshtmltextdocumenttype, nscharacterencodingdocumentattribute : nsutf8stringencoding], documentattributes: nil, error: nil)!.string } } let output = data.htmldata2string let components = output.componentsseparatedbystring("\n") index in stride(from: 1, to: 9, by: 3) { let line = components[index] let time = components[index+1] let delay = components[index+2] println( line + " - " + time + " - " + delay ) }
Comments
Post a Comment