swift - drmohundro/SWXMLHash 和 Swift 中的转义值
问题描述
我正在处理一个处理 xml 响应的项目。经过搜索,我在 github drmohundro/SWXMLHash上找到了一个受“Swifty JSON”启发的库。使用一段时间后,我意识到我无法通过转义值获取值。
xml 响应看起来像
let xmlResponseString = "<TrackList><Entry><Id>2</Id><Uri>/Input/E21u</Uri><Metadata><DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"><item id="E21"><dc:title>Doing It To Death</dc:title><upnp:album>Ash & Ice</upnp:album><upnp:artist>The Kills</upnp:artist><upnp:class>object.item.audioItem.musicTrack</upnp:class><upnp:albumArtURI>http://192.168.1.106:8088/storage/emulated/0/record_cache/album_art/1535461905688.jpg</upnp:albumArtURI><res sampleFrequency="96000" bitsPerSample="24" bitrate="2304000" nrAudioChannels="2" protocolInfo="http-get:*:audio/mpeg" duration="00:04:07.431">/Input/E21</res></item></DIDL-Lite></Metadata></Entry></TrackList>"
在响应中,专辑名称等于“Ash & Ice”。但是返回的值是“Ash”
这就是我获得价值的方式:
let xmlHash = SWXMLHash.parse(xmlResponseString)
albumName = xmlHash["DIDL-Lite"]["item"]["upnp:album"].element?.text
此外,检查“xmlHash”看起来错误已经来自“SWXMLHash.parse(xmlResponseString)”。
“xmlResponseString”是否需要转义?是图书馆没有正确处理的事情吗?有什么选择吗?
谢谢
编辑 响应来自另一个 OpenHome 提供商设备。
原来的回应是:
<TrackList>
<Entry>
<Id>2</Id>
<Uri>/Input/E21u</Uri>
<Metadata><DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"><item id="E21"><dc:title>Doing It To Death</dc:title><upnp:album>Ash & Ice</upnp:album><upnp:artist>The Kills</upnp:artist><upnp:class>object.item.audioItem.musicTrack</upnp:class><upnp:albumArtURI>http://192.168.1.106:8088/storage/emulated/0/record_cache/album_art/1535461905688.jpg</upnp:albumArtURI><res sampleFrequency="96000" bitsPerSample="24" bitrate="2304000" nrAudioChannels="2" protocolInfo="http-get:*:audio/mpeg" duration="00:04:07.431">/Input/E21</res></item></DIDL-Lite>
</Metadata>
</Entry>
根据开发人员的说法,Metadata 值已被转义,因为它是 XML 中的 XML。不确定这是否重要
因为我想创建一个通用解析函数来填充一个类,所以我创建了这个方法:
func unescapeXMLPredefinedCharacters(value:String?) -> String{
var audioString = ""
if value != nil{
audioString = value!
//Replace Unicode HTML Entity
audioString = audioString.replacingOccurrences(of: """, with: "\"")
audioString = audioString.replacingOccurrences(of: "&", with: "&")
audioString = audioString.replacingOccurrences(of: "'", with: "'")
audioString = audioString.replacingOccurrences(of: "<", with: "<")
audioString = audioString.replacingOccurrences(of: ">", with: ">")
//Replace Unicode Decimal
audioString = audioString.replacingOccurrences(of: """, with: "\"")
audioString = audioString.replacingOccurrences(of: "&", with: "&")
audioString = audioString.replacingOccurrences(of: "'", with: "'")
audioString = audioString.replacingOccurrences(of: "<", with: "<")
audioString = audioString.replacingOccurrences(of: ">", with: ">")
//Replace Unicode Hex
audioString = audioString.replacingOccurrences(of: """, with: "\"")
audioString = audioString.replacingOccurrences(of: "&", with: "&")
audioString = audioString.replacingOccurrences(of: "'", with: "'")
audioString = audioString.replacingOccurrences(of: "<", with: "<")
audioString = audioString.replacingOccurrences(of: ">", with: ">")
}
return audioString
}
它不知道哪种 unicode 类型已用于转义。
然后我从我原来的问题中得到答案
解决方案
问题是转义的内部 xml 在某种意义上已经是错误的,因为它包含&
字符(在 unicode 中),也许<
还有其他。
首先,您不应该转义 unicode 实体,例如或根本不转义,因为 XmlParser 会为您处理这些。&
<
然后,您应该将诸如等之类的 unicode 实体转义为诸如(而不是 to )之类的&
xml 实体,然后 xml 解析器将处理这些实体(见上文)&
&
推荐阅读
- mysql - SQL count() 多个 where 值
- python - 使用 Allauth 和扩展客户用户模型登录的 Django 似乎不允许使用电子邮件和密码登录
- lambda - lambda 在 C++14/17 上失败,适用于更高版本
- docker - 为什么在使用多个 Docker 主机时,我会收到 Unable to create container with image Unable to pull image 错误 pull image?
- php - 如何正确设置要在 GET 请求中使用的函数以获取持久的身份验证数据?
- javascript - 为什么名称显示为空?
- python - 如何在 Django ModelForms 中编写占位符?
- angular-material - 当我启动一个对话框时,背景位置已经改变
- python - 我的 mongodb 数据库需要很长时间才能查询
- excel - 插入散点图只有1个点,标题实际上是一个值