I received xml from javascript and parse it to the struct.
struct gcrInfoStruct {
var folderView = " "
var actionType = " "
var isProtocolReview = " "
var folder: [folderDetail] = []
}
How could i convert this struct back to xml in order to pass to javascript?
From your code I do not know what a folderDetail
type looks like, but let's suppose it is something like this:
struct folderDetail {
var desc:String
var value:String
}
I also do not know what the pattern of your XML is but let's suppose it is something like this:
<?xml version="1.0" encoding="UTF-8"?>
<gcrInfoStruct>
<folderView>Folder View String</folderView>
<actionType>Action Type String</actionType>
<isProtocolReview>True or False</isProtocolReview>
<folder>
<folder1desc>folder1value</folder1desc>
<folder2desc>folder2value</folder2desc>
</folder>
</gcrInfoStruct>"
If I'm guessing along the right lines then to generate the XML we simply need to generate a string that matches the pattern based on the struct, e.g.
extension gcrInfoStruct {
func xml() -> String {
var string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gcrInfoStruct><folderView>\(folderView)</folderView><actionType>\(actionType)</actionType><isProtocolReview>\(isProtocolReview)</isProtocolReview><folder>"
for i in self.folder {
string.appendContentsOf("<\(i.desc)>\(i.value)</\(i.desc)>")
}
string.appendContentsOf("</folder></gcrInfoStruct>")
return string
}
}
To implement we'd do something like this:
var gcr = gcrInfoStruct()
// update properties
let xml = gcr.xml()
But if we wanted to return XML as NSData then we'd add this additional code:
extension gcrInfoStruct {
func xmlData() -> NSData? {
return self.xml().dataUsingEncoding(NSUTF8StringEncoding)
}
}
and to implement we'd do something like this:
var gcr = gcrInfoStruct()
// update properties
let data = gcr.xmlData()
It might be that the folderDetail
is not simply a pair of strings or that the XML pattern varies but hopefully there is enough here to get you started.