Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift中的drmohundro/SWXMLHash和转义值_Swift_Xml_Swift4_Swxmlhash - Fatal编程技术网

Swift中的drmohundro/SWXMLHash和转义值

Swift中的drmohundro/SWXMLHash和转义值,swift,xml,swift4,swxmlhash,Swift,Xml,Swift4,Swxmlhash,我正在从事一个处理xml响应的项目。经过搜索,我在github上找到了一个库,它的灵感来自于“迅捷JSON”。使用它一段时间后,我意识到我无法通过转义值获得值 xml响应看起来像 let xmlResponseString = "<TrackList><Entry><Id>2</Id><Uri>/Input/E21u</Uri><Metadata><DIDL-Lite xmlns="urn:schemas-

我正在从事一个处理xml响应的项目。经过搜索,我在github上找到了一个库,它的灵感来自于“迅捷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>"
此外,检查“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>

2.
/输入/E21u
<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/”><项目id=“E21”和#62<dc:标题>;干到死</dc:标题><upnp:album>;灰烬&;冰</upnp:album><upnp:艺术家>;死亡人数<人/upnp:艺术家><upnp:62级;object.item.audioItem.musicTrack</upnp:62级<upnp:albumArtURI和#62;http://192.168.1.106:8088/storage/emulated/0/record_cache/album_art/1535461905688.jpg</upnp:albumArtURI和#62<res sampleFrequency=“96000”bitsPerSample=“24”bitrate=“2304000”nrAudioChannels=“2”protocolInfo=“http://get:::audio/mpeg”duration=“00:04:07.431”和#62/输入/E21和#60/res></第62项</DIDL Lite和#62;

根据开发人员的说法,元数据值已被转义,因为它是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

}
func unescapeXMLPredefinedCharacters(值:String?)->String{
var audioString=“”
如果值!=nil{
audioString=值!
//替换Unicode HTML实体
audioString=audioString.replacingOccurrences(of:“”和“\”)
audioString=audioString.replacingOccurrences(of:“&;”,带“&”)
audioString=audioString.replacingOccurrences(of:“&apos;”,with:”)
audioString=audioString.replacingOccurrences(of:,with:)
//替换Unicode十进制
audioString=audioString.replacingOccurrences(of:";”,带“\”)
audioString=audioString.replacingOccurrences(of:&;”,带“&”)
audioString=audioString.replacingOccurrences(of:';”,带“”)
audioString=audioString.replacingOccurrences(of:<;,with:)
//替换Unicode十六进制
audioString=audioString.replacingOccurrences(of:";,带“\”)
audioString=audioString.replacingOccurrences(of:&;”,带“&”)
audioString=audioString.replacingOccurrences(of:';,带“”)
audioString=audioString.replacingOccurrences(of:<;,with:)
}
返回音频字符串
}
它不知道使用了哪种unicode类型来进行取消逃避


然后我从我原来的问题中得到了答案,问题是转义的内部xml在某种意义上已经是错误的,它包含
&
字符(unicode),并且可能
根据Andreas的回答,我已经更改了函数

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

}

您从哪里获得
xmlResponseString
?它似乎不是有效的xml-文本节点不能包含未替换的符号,因此它应该是“Ash&;Ice”。您是否说“&”不应该在那里?我已经编辑了我的问题是的,“转义”内部xml在某种意义上已经是错误的,一旦您取消转义它,就会出现一个“&”,应该是“&;”。这个“&;”然后将由xml解析器(SWXMLHash)处理。其他xml实体(如“>”()等)也是如此,一旦它们位于xml文本元素的内容中,那么我是否必须取消对内部xml的扫描?内部xml是否被错误转义(内部xml为Unicode Decimal,内部xml为Unicode HTML实体)?您不应该取消对所有这些xml实体(如“&;”或”“等)的转义,您应该将“&;38;”等取消对“&;”的转义,而不是对“&;”(以及所有其他实体)的转义。然后,xml解析器将在内部处理这些“&;”内容
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

}