非常奇怪的问题,我觉得我错过了一些非常简单但却无法解决的问题。我正在尝试构建一个这样的XML主体:
let bodyString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><UserNameLoginRequest xmlns=\"\(PSS_XML_DEFAULT_NAMESPACE)\" from=\"\(LoginManager.clientID)\"><UserName>\(username)</UserName><UserPassword>\(password)</UserPassword><ClientType>\(clientType)</ClientType></UserNameLoginRequest>"
但结果是(URL和凭据编辑):
<?xml version=\"1.0\" encoding= \"UTF-8\"?><UserNameLoginRequest xmlns=\"http://example.com/\" from=\"myDeviceID\"><UserName>000</UserName><UserPassword>1234</UserPassword><ClientType>myClientType</ClientType></UserNameLoginRequest>
注意反斜杠。显然这不是有效的XML,也不起作用。那么......我错过了什么?
答案 0 :(得分:1)
let username = "user"
let password = "pswd"
let clientType = 0
struct LoginManager {
static let clientID = 100
}
let bodyString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><UserNameLoginRequest xmlns=\"\\(PSS_XML_DEFAULT_NAMESPACE)\" from=\"\(LoginManager.clientID)\"><UserName>\(username)</UserName><UserPassword>\(password)</UserPassword><ClientType>\(clientType)</ClientType></UserNameLoginRequest>"
print(bodyString)
反斜杠也必须逃脱!!!
<?xml version="1.0" encoding="UTF-8"?><UserNameLoginRequest xmlns="\(PSS_XML_DEFAULT_NAMESPACE)" from="100"><UserName>user</UserName><UserPassword>pswd</UserPassword><ClientType>0</ClientType></UserNameLoginRequest>
例如
print("a\"\\b") //prints a"\b