Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift warm up xmlelement

Avatar for Johnlin Johnlin
April 03, 2018

swift warm up xmlelement

Avatar for Johnlin

Johnlin

April 03, 2018
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

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

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

    attribute: Element తಛੑ • comment: Ḽղ • ଖଞɿ࿨ᱛᨽ༗᮫(DTD)
  3. 㗞ੜ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"])
  4. XMLParser • Delegate based streaming parser. • Delegate based =>

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

    best </is> </swift> """ let data = xmlText.data(using: .utf8) let parser = XMLParser(data: data!)
  6. ࢼ༻ 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)") } }
  7. ࢼ༻ 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
  8. ᫚ग़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)!) •
  9. ᫚ग़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> •
  10. ࣗగ᫚ग़త Key struct People : Codable { let name :

    String let foods : [String] enum CodingKeys: String, CodingKey { case name = "full_name" case foods } }
  11. ࣗగ᫚ग़త 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>
  12. ኺ Plist ᫚ճ object let decoder = PropertyListDecoder() let another_john

    = try? decoder.decode(People.self, from: encoded!) • ။ࣗಈ൑Ꮧ plist ֨ࣜɼ࠶᫚੒ data
  13. Q&A