chenyunguiMilook / SwiftyXML

The most swifty way to deal with XML data in swift 5.
MIT License
101 stars 27 forks source link

How to make escaping sequence String from xml? #8

Closed kiri11ko closed 5 years ago

kiri11ko commented 5 years ago

How to make escaping sequence String from XML? For write-in file and send on the server. Thanks.

chenyunguiMilook commented 5 years ago

Do you mean to ask this? https://stackoverflow.com/questions/33860624/escaping-strings-in-swift

kiri11ko commented 5 years ago

Yes, I need to check all the value? Does Library no have the function for this case?

kiri11ko commented 5 years ago

How to get all value for the check?

chenyunguiMilook commented 5 years ago

Yes, this library will not handle this case, you can escape the whole XML string for you use case~

kiri11ko commented 5 years ago

OK. Then do not write what you can create XML as it is not. I get XML and add new keys to it, but I need to shovel it all to remake special characters since your library returns just a string.

kiri11ko commented 5 years ago

For make true XML need in the loop get all value and fix then. Example: let newXMLHead: XML = XML(name: "clients") let usersXML: [XML] = memoryXML!["#clients"]["client"].xmlList! for value in usersXML { let newXML: XML = XML(name: "clients") for (key, subValue) in value.attributes { newXML.addAttribute(name: key, value: subValue.percentEscapeString()) } newXMLHead.addChild(newXML) }

Where extension String { func percentEscapeString() -> String { return self .replacingOccurrences(of: "&", with: "\&") .replacingOccurrences(of: "\"", with: "\"") .replacingOccurrences(of: "'", with: "\'") .replacingOccurrences(of: ">", with: "\>") .replacingOccurrences(of: "<", with: "\<") } }