1 Reply
      Latest reply: Dec 11, 2016 3:37 PM by eskimo RSS
      n0kx Level 1 Level 1 (0 points)

        Hello. I'm seeing a lot online about how to initialize XMLParser but not on how to use the data.

         

        Basically, I have this so far in my ViewController:

         

        class MyViewController: UIViewController, XMLParserDelegate {
             @IBOutlet weak var personName: UILabel!
        
             override func viewDidLoad() {
                  super.viewDidLoad()
        
                  let dataURL = URL(string: "http://mysite.com/data.xml")
                  let parser = XMLParser(contentsOf: dataURL)
                  
                  parser?.delegate = self
                  parser?.parse()
             }
        }
        

         

        XMLParser returns true, but I'm not sure what to do now about populating the UILabel personName.

         

        We can say the XML looks something like this:

         

        <Person>
             <Name>Bob</Name>
        </Person>
        

         

        Any tips on the best practices on populating objects with the data? Thanks!

        • Re: A simple example with XMLParser
          eskimo Apple Staff Apple Staff (6,470 points)

          When you call the parse() method, XMLParser walks through the elements in the XML calling your delegate as it finds things.  You have to implement these delegate methods to extract the parts of the XML your care about.  I posted more about this here.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"