How do I pass a url to NSBundle for xml parsing with AEXML?

蹲街弑〆低调 提交于 2020-01-25 23:11:13

问题


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.

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.

How do I pass a url to NSBundle for xml parsing?

Relevant code:

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)

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


回答1:


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

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

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()
}


来源:https://stackoverflow.com/questions/29823077/how-do-i-pass-a-url-to-nsbundle-for-xml-parsing-with-aexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!