【问题标题】:Swift - remove namespaces from XML stringSwift - 从 XML 字符串中删除命名空间
【发布时间】:2015-04-20 02:53:23
【问题描述】:

我有一个带有命名空间的 SOAP 回复,像这样

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:doLoginResponse xmlns:ns2="http://mobile.ws.legal.org/"><result>true</result></ns2:doLoginResponse></S:Body></S:Envelope>

是否可以删除 xml/envelope/body 标记并从剩余部分中删除“ns2”命名空间?

谢谢

【问题讨论】:

  • 是的。但你真正的问题是什么?
  • 你应该把它作为答案。只是“是” - 大声笑

标签: ios xml xcode swift namespaces


【解决方案1】:

预测您的预期问题:您可以使用正则表达式来删除该模糊。

let string = "<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:doLoginResponse xmlns:ns2=\"http://mobile.ws.legal.org/\"><result>true</result></ns2:doLoginResponse></S:Body></S:Envelope>here's the dish"
var error:NSError?
let regex = NSRegularExpression(pattern:"<\\?xml version.*</S:Envelope>", options:.CaseInsensitive, error:&error)!
let modifiedString = regex.stringByReplacingMatchesInString(string,     options:  NSMatchingOptions.allZeros, range: NSMakeRange(0, countElements(string)), withTemplate:"")
println(modifiedString)

【讨论】: