簡體   English   中英

Swift中的drmohundro / SWXMLHash和轉義值

[英]drmohundro/SWXMLHash and escaping values in 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>&#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>

根據開發人員的說法,元數據值已轉義,因為它是XML內的XML。 不確定這是否重要

由於我想創建一個通用的解析函數來填充類,因此我創建了以下方法:

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

}

它不知道哪種unicode類型用於轉義。

然后我從原來的問題中得到答案

問題在於, 轉義的內部xml在某種意義上已經是錯誤的,因為它包含&字符(以unicode表示),並且可能包含<和其他字符。

首先,您不應該 Unicode代碼實體(例如&amp; &lt; 根本沒有,因為XmlParser會為您處理此問題。

然后,您應該取消對&#38;類的unicode實體進行轉義&#38; 等到xml實體(如&amp; (而不是& ),然后由XML解析器處理(請參見上文)

根據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