简体   繁体   English

Swift中的drmohundro / SWXMLHash和转义值

[英]drmohundro/SWXMLHash and escaping values in Swift

I have working on a project that handles xml responses. 我正在处理XML响应的项目​​。 After a search I found a library on github drmohundro/SWXMLHash that works inspired on the "Swifty JSON". 搜索之后,我在github drmohundro / SWXMLHash上找到了一个库,该库的灵感来自“ Swifty JSON”。 After a while using it, I have realised that I can't get values with escaping values. 使用一段时间后,我意识到我无法获得带有转义值的值。

The xml response looks like 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>"

In the response the album name is equal to "Ash & Ice". 在响应中,专辑名称等于“ Ash&Ice”。 However the value returned is "Ash " 但是,返回的值为“ Ash”

That is how I get the value: 这就是我获得价值的方式:

let xmlHash = SWXMLHash.parse(xmlResponseString)
albumName = xmlHash["DIDL-Lite"]["item"]["upnp:album"].element?.text

Furthermore, inspecting "xmlHash" it looks like the error already comes from "SWXMLHash.parse(xmlResponseString)". 此外,检查“ xmlHash”似乎该错误已经来自“ SWXMLHash.parse(xmlResponseString)”。

Does the "xmlResponseString" need to be escaped? 是否需要对“ xmlResponseString”进行转义? Is it something that the library doesn't handle properly? 库无法正确处理吗? Any alternative? 还有其他选择吗?

Thank you 谢谢

EDIT The response comes from another OpenHome provider device. 编辑响应来自另一个OpenHome提供程序设备。

The original response is: 原始响应是:

<TrackList>
<Entry>
    <Id>2</Id>
    <Uri>/Input/E21u</Uri>
    <Metadata>&#60;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/"&#62;&#60;item id="E21"&#62;&#60;dc:title&#62;Doing It To Death&#60;/dc:title&#62;&#60;upnp:album&#62;Ash &#38; Ice&#60;/upnp:album&#62;&#60;upnp:artist&#62;The Kills&#60;/upnp:artist&#62;&#60;upnp:class&#62;object.item.audioItem.musicTrack&#60;/upnp:class&#62;&#60;upnp:albumArtURI&#62;http://192.168.1.106:8088/storage/emulated/0/record_cache/album_art/1535461905688.jpg&#60;/upnp:albumArtURI&#62;&#60;res sampleFrequency="96000" bitsPerSample="24" bitrate="2304000" nrAudioChannels="2" protocolInfo="http-get:*:audio/mpeg" duration="00:04:07.431"&#62;/Input/E21&#60;/res&#62;&#60;/item&#62;&#60;/DIDL-Lite&#62;
    </Metadata>
</Entry>

According to the developer, the Metadata value has been escaped because it is an XML inside an XML. 根据开发人员的说法,元数据值已转义,因为它是XML内的XML。 Not sure if that matter 不确定这是否重要

Since I want to create an universal parse function to populate a class, I have created this method: 由于我想创建一个通用的解析函数来填充类,因此我创建了以下方法:

func unescapeXMLPredefinedCharacters(value:String?) -> String{

var audioString = ""

if value != nil{

    audioString = value!

    //Replace Unicode HTML Entity
    audioString = audioString.replacingOccurrences(of: "&quot;", with: "\"")
    audioString = audioString.replacingOccurrences(of: "&amp;", with: "&")
    audioString = audioString.replacingOccurrences(of: "&apos;", with: "'")
    audioString = audioString.replacingOccurrences(of: "&lt;", with: "<")
    audioString = audioString.replacingOccurrences(of: "&gt;", with: ">")


    //Replace Unicode Decimal
    audioString = audioString.replacingOccurrences(of: "&#34;", with: "\"")

    audioString = audioString.replacingOccurrences(of: "&#38;", with: "&")
    audioString = audioString.replacingOccurrences(of: "&#39;", with: "'")
    audioString = audioString.replacingOccurrences(of: "&#60;", with: "<")
    audioString = audioString.replacingOccurrences(of: "&#62;", with: ">")


    //Replace Unicode Hex
    audioString = audioString.replacingOccurrences(of: "&#x22;", with: "\"")
    audioString = audioString.replacingOccurrences(of: "&#x26;", with: "&")
    audioString = audioString.replacingOccurrences(of: "&#x27;", with: "'")
    audioString = audioString.replacingOccurrences(of: "&#x3c;", with: "<")
    audioString = audioString.replacingOccurrences(of: "&#x3e;", with: ">")

}

return audioString

}

It doesn't know which unicode type has been used for the unescaping. 它不知道哪种unicode类型用于转义。

Then I get the answer from my original question 然后我从原来的问题中得到答案

The problem is that the escaped inner xml already is wrong in a sense that it contains & characters (in unicode), and maybe < and others. 问题在于, 转义的内部xml在某种意义上已经是错误的,因为它包含&字符(以unicode表示),并且可能包含<和其他字符。

First, you should not unescape unicode entities like &amp; 首先,您不应该 Unicode代码实体(例如&amp; or &lt; &lt; at all, because the XmlParser handles this for you. 根本没有,因为XmlParser会为您处理此问题。

Then, you should unescape unicode entities like &#38; 然后,您应该取消对&#38;类的unicode实体进行转义&#38; etc. into xml entities like &amp; 等到xml实体(如&amp; (rather than to & ), which then the xml parser will handle (see above) (而不是& ),然后由XML解析器处理(请参见上文)

According to Andreas Answer, I have change the function 根据Andreas Answer,我已经更改了功能

func unescapeXMLPredefinedCharacters(value:String?) -> String{

    var audioString = ""

    if value != nil{

        audioString = value!

        //Replace Unicode Decimal
        audioString = audioString.replacingOccurrences(of: "&#34;", with: "&quot;")
        audioString = audioString.replacingOccurrences(of: "&#38;", with: "&amp;")
        audioString = audioString.replacingOccurrences(of: "&#39;", with: "&apos;")
        audioString = audioString.replacingOccurrences(of: "&#60;", with: "&lt;")
        audioString = audioString.replacingOccurrences(of: "&#62;", with: "&gt;")

    }

    return audioString

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM