swift warm up xmlelement

3f7d9611fc919c98512b779cde637dfc?s=47 Johnlin
April 03, 2018

swift warm up xmlelement

3f7d9611fc919c98512b779cde637dfc?s=128

Johnlin

April 03, 2018
Tweet

Transcript

  1. Swift ఔࣜখ೤਎ XMLElement John Lin

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

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

  4. XML ߏ଄ • <aaa>
 <bbb xxx="yyy">
 ccc
 </bbb>
 </aaa>

  5. XMLNode • ॴ༗ XML ૬᮫త݁ߏ౎ੋṜݸ class త subclass • ՄҎ㗞ੜ֤छ

    subclass • ༗ Document, element, attribute, text, comment, namespace, dtd ౳౳
  6. Nodes • Document: ୅ද੔ݸจ݅ɼ။༗Ұݸ root element • Text: Ұஈจࣈ •

    attribute: Element తಛੑ • comment: Ḽղ • ଖଞɿ࿨ᱛᨽ༗᮫(DTD)
  7. 㗞ੜXML • <aaa>
 <bbb xxx="yyy">
 ccc
 </bbb>
 </aaa> var aaa

    = XMLElement(name: "aaa") var bbb = XMLElement(name: "bbb", stringValue: "ccc") aaa.addChild(bbb) bbb.setAttributesWith(["xxx": "yyy"])
  8. ኺXML Ꮣ੒ String • ୞ཁݺڣ String(describing:) print(String(describing: aaa)) <aaa><bbb xxx="yyy">ccc</bbb></aaa>

  9. XMLParser • Delegate based streaming parser. • Delegate based =>

    ෆ။௚઀ࠂૌ㟬݁Ռɼ୞။ࠂૌ 㟬ଞ۰౸ྃॄኄ • streaming parser => ෆधཁᩇ౸࠷ޙҰݸࣈ࠽။༗ ݁Ռ • ؆ᄸိ㘸बੋ኷೉༻
  10. ࢼ༻ XMLParser let xmlText = """ <swift> <is cool=\"true\"> the

    best </is> </swift> """ let data = xmlText.data(using: .utf8) let parser = XMLParser(data: data!)
  11. ࢼ༻ 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)") } }
  12. ࢼ༻ 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
  13. Plist • Plist ༗ࡾछ ASCII, XML, Binary • Plist ࢧԉ

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

  15. ᫚ग़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)!) •
  16. ᫚ग़XML Plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST

    1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>foods</key> <array> <string>pudin</string> <string>cake</string> </array> <key>name</key> <string>john</string> </dict> </plist> •
  17. ࣗగ᫚ग़త Key struct People : Codable { let name :

    String let foods : [String] enum CodingKeys: String, CodingKey { case name = "full_name" case foods } }
  18. ࣗగ᫚ग़త Key <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST

    1.0//EN" "http:// www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>foods</key> <array> <string>pudin</string> <string>cake</string> </array> <key>full_name</key> <string>john</string> </dict> </plist>
  19. ኺ Plist ᫚ճ object let decoder = PropertyListDecoder() let another_john

    = try? decoder.decode(People.self, from: encoded!) • ။ࣗಈ൑Ꮧ plist ֨ࣜɼ࠶᫚੒ data
  20. ݁࿦ • XMLElement ᕝ೉༻త • ཁଘ䈕త࿩ؐੋ༻ Codable ഑ JSON/Plist 㠧

  21. Q&A

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