Swift As Well As Xml Episode 1: Defining Xml Using Swift


XML is a really elementary linguistic communication at heart. It contains elements, i.e. those types that are original to a tag, e.g. <paragraph> or <p>, as well as which are also used to closed tags, e.g. </paragraph> or </p>, it so has attributes, which are secondary but which are also contained inside the boundaries of the the tag, e.g. class="regular", which is contained similar so <p class="regular">. Finally nosotros convey the content, which tin survive 1 of 2 things: text or to a greater extent than or less other chemical part nested inside the first.

Since everything begins inwards XML alongside the top-level element, that's what volition start alongside too:
struct Element {     permit name:String     permit attributes:[Atrribute]     permit content:[Content]     var self_closing:Bool {         furnish content.isEmpty     } } 
We convey instantly the nearly of import thing, our powerfulness to practise instances of an Element, nosotros also convey the powerfulness to survive aware if the chemical part nosotros are dealing alongside has no content as well as volition so involve self-closing, but that's exactly making things easier for us later. The matter nosotros involve to practise instantly is to define what an Attribute is as well as what Content looks similar inwards Swift. An Attribute is the easiest one:
struct Attribute {     permit name:String     permit value:String } 
For the Content I'm going to employ an enum alongside associated values:
enum Content {     instance element(Element), text(String) } 
Now nosotros convey everything nosotros involve to impose a generic XML construction on our Swift code. For example:
let a = Element(name:"a", attributes: [Attribute(name:"href", value:"https://blogspot.sketchytech.co.uk")], content:[.text("visit  today!")]) permit p = Element(name:"p", attributes: [], content:[.text("It's fourth dimension to "), .element(a), .text(" You won't regret it!"]) 
And I approximate that since we've come upwardly this far nosotros mightiness every bit good write a method for transforming our XML-structured Swift into, good you lot know, XML. So to practise that:
extension Element {     func outputXML() -> String {         var str = "<\(name)"         for a inwards attributes {             str += "\(a.name)="\(a.value)\""         }         if self_closing {             str += " />"         }         else {             str += ">"             for c inwards content {                 switch c {                 instance .element(let e):                     str += e.outputXML()                 instance .text(let t):                     str += text                 }             }             str += "</\(name)>"         }         furnish str     } } 
We tin campaign this on the Element instances nosotros already created past times but writing:
let xml = p.outputXML() print(xml) 
as well as nosotros tin also evidence the self-closing:
let i = Element(name:"img", attributes: [Attribute(name:"src", value:"image.png")], content:[]) permit xml = i.outputXML() print(xml) 
You should run into that all is working well. Next time, I'll survive leveraging the Codable protocol inwards lodge to transform our XML into JSON as well as transform that JSON dorsum into XML.

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?