问题在Swift中从xml文件解码字符串

时间:2018-09-17 02:58:48

标签: arrays swift xml

我有一个内容如下的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="array_ngontay_kq">
        <item>Bạn là người đáng tin cậy.</item>
        <item>Bạn là người có óc xét đoán. </item>
    </string-array>
</resources>

然后我用HTMLReader得到了这个字符串数组,但是我的输出看起来像这样:

Bạn là ngÆ°á»i Äáng tin cậy.
Bạn là ngÆ°á»i có óc xét Äoán.

这是我的代码:

let fileURL = Bundle.main.url(forResource: "BoiTay", withExtension: "xml")
        let xmlData = try! Data(contentsOf: fileURL!)
        let topic = "array_ngontay_kq"
        let document = HTMLDocument(data: xmlData, contentTypeHeader: "text/xml")
        for item in document.nodes(matchingSelector: "string-array[name='\(topic)'] item") {
                print(item.textContent)
            }

是否有解决此问题的方法,或者任何其他解决方案都可以在不使用HTMLReader的情况下做到这一点。对不起,我是XMLParse的新人,我在Swift中找不到关于这种xml文件的任何答案或教程。

1 个答案:

答案 0 :(得分:0)

首先,您最好检查您的commit(keyMutation, payload) { (arguments.length < 2) ? this.mutations[keyMutation](this.state) : this.mutations[keyMutation](this.state, payload); }, 是否真的在UTF-8中。我对越南语编码不熟悉,但是即使xml标头声明为BoiTay.xml,某些工具也可能会使用UTF-8以外的其他编码生成XML。

结果似乎是编码问题,而不是库或代码的错误。

请显示您的encoding="utf-8"的十六进制转储,包括第一个xmlData元素。

item

也许前256个字节就足够了。


顺便说一句,使用print(xmlData as NSData) 并不是那么困难。 (尽管这并不容易。)

这是您可以在Playground中测试的示例。

XMLParser

输出:

import Foundation

class ResoucesParsingDelegate: NSObject, XMLParserDelegate {
    //Property to keey all `string-array`s by name
    var stringArrays: [String: [String]] = [:]

    var nameParsing: String? = nil
    var stringArrayParsing: [String]? = nil

    var currentText: String? = nil

    func parserDidStartDocument(_ parser: XMLParser) {
        print(#function)
    }

    func parserDidEndDocument(_ parser: XMLParser) {
        print(#function)
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        print(#function, parseError)
    }

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        switch elementName {
        case "string-array":
            guard let name = attributeDict["name"] else {
                print("`string-array` element needs `name` attribute")
                return
            }
            //When you find `<string-array name="...">`, prepare a string array to keep items with its name
            nameParsing = name
            stringArrayParsing = []
        case "item":
            if stringArrayParsing == nil {
                print("invalid `item` element")
                return
            }
            //When you find `<item>`, prepare a string to keep the content text of the element
            currentText = ""
        //Prodess other elements
        //...
        default:
            print("Unknown element `\(elementName)`, ignored")
        }
    }

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        switch elementName {
        case "string-array":
            if stringArrayParsing == nil || nameParsing == nil {
                print("invalid end tag `string-array`")
                return
            }
            //When you find `</string-array>`, add the current string array to `stringArrays` with its name
            stringArrays[nameParsing!] = stringArrayParsing!
            //Clear string array for next use
            stringArrayParsing = nil
        case "item":
            if stringArrayParsing == nil || currentText == nil {
                print("invalid end tag `item`")
                return
            }
            //When you find `</item>` add the content text to `stringArrayParsing`
            stringArrayParsing!.append(currentText!)
            //Clear content text for next use
            currentText = nil
        //Prodess other elements
        //...
        default:
            print("Unknown element `\(elementName)`, ignored")
        }
    }

    func parser(_ parser: XMLParser, foundCharacters string: String) {
        if currentText == nil {
            //Silently igonore characters while content string is not ready
            return
        }
        currentText! += string
    }
}

let xmlText = """
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="array_ngontay_kq">
        <item>Bạn là người đáng tin cậy.</item>
        <item>Bạn là người có óc xét đoán. </item>
    </string-array>
</resources>
"""

let xmlData = xmlText.data(using: .utf8)!

print(xmlData, xmlData as NSData)

let parser = XMLParser(data: xmlData)
let resoucesParsingDelegate = ResoucesParsingDelegate()
parser.delegate = resoucesParsingDelegate
parser.parse()

print(resoucesParsingDelegate.stringArrays)

如果使用246 bytes <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d227574 662d3822 3f3e0a3c 7265736f 75726365 733e0a20 2020203c 73747269 6e672d61 72726179 206e616d 653d2261 72726179 5f6e676f 6e746179 5f6b7122 3e0a2020 20202020 20203c69 74656d3e 42e1baa1 6e206cc3 a0206e67 c6b0e1bb 9d6920c4 91c3a16e 67207469 6e2063e1 baad792e 3c2f6974 656d3e0a 20202020 20202020 3c697465 6d3e42e1 baa16e20 6cc3a020 6e67c6b0 e1bb9d69 2063c3b3 20c3b363 2078c3a9 7420c491 6fc3a16e 2e203c2f 6974656d 3e0a2020 20203c2f 73747269 6e672d61 72726179 3e0a3c2f 7265736f 75726365 733e> parserDidStartDocument Unknown element `resources`, ignored Unknown element `resources`, ignored parserDidEndDocument ["array_ngontay_kq": ["Bạn là người đáng tin cậy.", "Bạn là người có óc xét đoán. "]] 的内容测试此代码并获得与BoiTay.xml类似的结果,则问题肯定是编码问题。

(如果实际的xml比示例复杂,则可能需要修改此代码。)