NSXMLParser:非ASCII字符的意外结果 [英] NSXMLParser: Unexpected result with non-ASCII characters

查看:75
本文介绍了NSXMLParser:非ASCII字符的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过REST Web服务以XML格式下载数据。
我已经用NSXMLParser创建了解析器并在TableView中可视化数据。

I'm trying to download data in XML format via a REST web services. I have already created the parser with NSXMLParser and visualize the data in a TableView.

当我遇到带有重音的XML文档句子时,我遇到了问题。

I have problems when I meet in the XML document sentences with the accent.

在一些研究中,我发现这个与我的问题非常相似并试图实现它:

Among some research I found this very similar to my problem and tried to implement it:

func parse(handler: () -> Void) {
    self.handler = handler
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        self.countCategoryScheme = 0
        var url = NSURL(string: SomeStructure.firstLink);
        var err:NSError = NSError()
        var dataString: String = String(contentsOfURL: url!, encoding: NSUTF8StringEncoding, error: nil)!
        var data: NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
        let parser = NSXMLParser(data: data)
        let resulset = NSString(data: data, encoding: NSUTF8StringEncoding)
        println(resulset)
        parser.delegate = self;
        parser.parse();
        if !parser.parse() {
            self.delegate?.XMLParserError(self, error: "I Parse Error")
            println("I Parser error")
        }
    }
}

in println(resulset) 我打印正确解码的整个XML文件。

in println (resulset) I print the entire XML file correctly decoded.

问题是当我运行解析器时,未读取带重音的字符

The problem is when I run the parser, the accented characters are not read

当我在解析器中找到字符时,这是代码:

This is code when i found Characters in the parser:

   func parser(parser: NSXMLParser!, foundCharacters string: String!) {
        myList[position] = string
    }

编辑:

这是我的文档示例:

<Name xml:lang="en" xmlns="">National Accounts</Name>

<Name xml:lang="it" xmlns="">Contabilità nazionale</Name>

在println()中,如上所述正确打印文档。

In println () print the document correctly as described above.

相反,当我去保存找到carachter的数据时,如果它为我节省了这个:

Instead, when I go to save the data found carachter if "it" saves me this:

Contabilit

推荐答案

解析器:foundCharacters:委托方法可以被调用超过
一次单个XML元素。在你的例子中,它将被称为
一次使用Contabilit,一次使用ànazionale。

The parser:foundCharacters: delegate method can be called more than once for a single XML element. In your example, it would be called once with "Contabilit", and once with "à nazionale".

因此你必须连接
didStartElement 找到的字符串,直到 didEndElement

Therefore you have to concatenate the found strings from didStartElement until didEndElement.

这是一个非常简单的例子,说明如何做到这一点。当然,如果你有嵌套的XML元素,它会让
更复杂。

Here is a very simply example how this can be done. Of course it gets more complicated if you have nested XML elements.

为你的类添加当前元素字符串的属性:

Add a property for the current element string to your class:

var currentElement : String?

然后实现这样的委托方法:

And then implement the delegate methods like this:

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {

    // If a "Name" element started (which you are interested in), set
    // currentElement to an empty string, so that the found characters
    // can be collected. Otherwise set it to nil.
    if elementName == "Name" {
        currentElement = ""
    } else {
        currentElement = nil
    }

}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

    // If the "Name" element ended, get the collected string and
    // append it to your list.
    if elementName == "Name" {
        if let name = currentElement {
            println(name)
            myList.append(name)
        }
    }
    currentElement = nil
}

func parser(parser: NSXMLParser, foundCharacters string: String?) {

    // If currentElement is not nil, append the found characters to it:
    currentElement? += string ?? ""
}

这篇关于NSXMLParser:非ASCII字符的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆