Slide 1

Slide 1 text

Swift ఔࣜখ೤਎ XMLElement John Lin

Slide 2

Slide 2 text

XMLElement • XML จ݅ཫతҰݸ Node, Լ໘ՄҎ༗ߋଟత Node

Slide 3

Slide 3 text

XML • ௕త኷૾ HTML ɼෆաੋ፤ိଘࢿྉత • ࠷ۙᔒॄኄਓ༻ • େՈ౎վ༻ json ྃ

Slide 4

Slide 4 text

XML ߏ଄ • 
 
 ccc
 


Slide 5

Slide 5 text

XMLNode • ॴ༗ XML ૬᮫త݁ߏ౎ੋṜݸ class త subclass • ՄҎ㗞ੜ֤छ subclass • ༗ Document, element, attribute, text, comment, namespace, dtd ౳౳

Slide 6

Slide 6 text

Nodes • Document: ୅ද੔ݸจ݅ɼ။༗Ұݸ root element • Text: Ұஈจࣈ • attribute: Element తಛੑ • comment: Ḽղ • ଖଞɿ࿨ᱛᨽ༗᮫(DTD)

Slide 7

Slide 7 text

㗞ੜXML • 
 
 ccc
 
 var aaa = XMLElement(name: "aaa") var bbb = XMLElement(name: "bbb", stringValue: "ccc") aaa.addChild(bbb) bbb.setAttributesWith(["xxx": "yyy"])

Slide 8

Slide 8 text

ኺXML Ꮣ੒ String • ୞ཁݺڣ String(describing:) print(String(describing: aaa)) ccc

Slide 9

Slide 9 text

XMLParser • Delegate based streaming parser. • Delegate based => ෆ။௚઀ࠂૌ㟬݁Ռɼ୞။ࠂૌ 㟬ଞ۰౸ྃॄኄ • streaming parser => ෆधཁᩇ౸࠷ޙҰݸࣈ࠽။༗ ݁Ռ • ؆ᄸိ㘸बੋ኷೉༻

Slide 10

Slide 10 text

ࢼ༻ XMLParser let xmlText = """ the best """ let data = xmlText.data(using: .utf8) let parser = XMLParser(data: data!)

Slide 11

Slide 11 text

ࢼ༻ XMLParser @objc class Reader : NSObject, XMLParserDelegate { func parserDidStartDocument(_ parser: XMLParser) { print("doc started") } func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { print("got elem \(elementName) with attr \ (attributeDict)") } func parser(_ parser: XMLParser, foundCharacters string: String) { print("got value \(string)") } }

Slide 12

Slide 12 text

ࢼ༻ XMLParser let delegate = Reader() parser.delegate = delegate parser.parse() output: doc started got elem swift with attr [:] got value got elem is with attr ["cool": "true"] got value the best got value

Slide 13

Slide 13 text

Plist • Plist ༗ࡾछ ASCII, XML, Binary • Plist ࢧԉ Codable !!

Slide 14

Slide 14 text

PropertyListEncoder PropertyListDecoder • ࢧԉ Encode & Decode • ՄҎ༻ Codable

Slide 15

Slide 15 text

᫚ग़XML Plist struct People : Codable { let name : String let foods : [String] } let john = People(name: "john", foods: ["pudin", "cake"]) let encoder = PropertyListEncoder() encoder.outputFormat = .xml let encoded = try? encoder.encode(john) print(String(data: encoded!, encoding: .utf8)!) •

Slide 16

Slide 16 text

᫚ग़XML Plist foods pudin cake name john •

Slide 17

Slide 17 text

ࣗగ᫚ग़త Key struct People : Codable { let name : String let foods : [String] enum CodingKeys: String, CodingKey { case name = "full_name" case foods } }

Slide 18

Slide 18 text

ࣗగ᫚ग़త Key foods pudin cake full_name john

Slide 19

Slide 19 text

ኺ Plist ᫚ճ object let decoder = PropertyListDecoder() let another_john = try? decoder.decode(People.self, from: encoded!) • ။ࣗಈ൑Ꮧ plist ֨ࣜɼ࠶᫚੒ data

Slide 20

Slide 20 text

݁࿦ • XMLElement ᕝ೉༻త • ཁଘ䈕త࿩ؐੋ༻ Codable ഑ JSON/Plist 㠧

Slide 21

Slide 21 text

Q&A

Slide 22

Slide 22 text

፮ᐔػ • http://slot.miario.com/machines/150067