如何将 url 传递给 NSBundle 以使用 AEXML 进行 xml 解析? [英] How do I pass a url to NSBundle for xml parsing with AEXML?

查看:62
本文介绍了如何将 url 传递给 NSBundle 以使用 AEXML 进行 xml 解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AEXML 并希望将 XML 从 REST 服务传递到 AEXML 解析器.但是,在提供的示例代码中,作者将一个 local xml 文件传递​​到他的解析器中.根据提供的示例,我似乎无法弄清楚如何传递通过 URL 接收的 xml 数据.

I'm using AEXML and would like to pass XML from a REST service into the AEXML parser. However, in the example code provided the author passes a local xml file into his parser. Based on the example provided I can't seem to figure out how to pass xml data that was received via URL.

在将它传递给捆绑器之前,我尝试捕获 url 并使用 NSXMLParser 对其进行解析,但没有奏效.我还尝试从 URL 流创建一个 NSData 对象.所以这让我想到了这个问题.

I have tried capturing the url and parsing it with NSXMLParser before passing it to the bundler but that didn't work. I've also tried creating an NSData object from the URL stream. So it leads me to the question.

如何将 url 传递给 NSBundle 进行 xml 解析?

相关代码:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // example from README.md
        if let xmlPath = NSBundle.mainBundle().pathForResource("example", ofType: "xml") {
            if let data = NSData(contentsOfFile: xmlPath) {

                // works only if data is successfully parsed
                // otherwise prints information about error with parsing
                var error: NSError?
                if let xmlDoc = AEXMLDocument(xmlData: data, error: &error) {

                    // prints the same XML structure as original
                    println(xmlDoc.xmlString)

                    // prints cats, dogs
                    for child in xmlDoc.root.children {
                        println(child.name)
                    }

                    // prints Optional("Tinna") (first element)
                    println(xmlDoc.root["cats"]["cat"].value)

                    // prints Tinna (first element)
                    println(xmlDoc.root["cats"]["cat"].stringValue)

如果需要查看更多代码,可以在GitHub上查看这里.

if you need to see more code, you can view it on GitHub here.

推荐答案

不能将包含远程数据的 URL 传递给 AEXML,因为它不做网络,只做 XML 解析.

You can't pass URL containing remote data to AEXML, because it does not do networking, only XML parsing.

您应该首先从您的 URL 创建 NSData,然后从该数据创建 AEXMLDocument.

You should first create NSData from your URL and create AEXMLDocument from that data.

如果您查看 AEXMLExample 项目内部,您会找到执行此操作的确切示例,如果您在模拟器中运行它,您也可以使用任何 URL 进行尝试:

If you look inside the AEXMLExample project, you will find exact example for doing just that, and you can also try it with any URL if you run it in simulator:

@IBAction func tryRemoteXML(sender: UIButton) {
    if let url = NSURL(string: textField.text) {
        if let data = NSData(contentsOfURL: url) {
            var error: NSError?
            if let doc = AEXMLDocument(xmlData: data, error: &error) {
                var parsedText = String()
                // parse unknown structure
                for child in doc.root.children {
                    parsedText += child.xmlString + "\n"
                }
                textView.text = parsedText
            } else {
                let err = "description: \(error?.localizedDescription)\ninfo: \(error?.userInfo)"
                textView.text = err
            }
        }
    }
    textField.resignFirstResponder()
}

这篇关于如何将 url 传递给 NSBundle 以使用 AEXML 进行 xml 解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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