使用Swift(IOS)进行xml解析帮助我

时间:2016-05-09 15:21:26

标签: ios xml swift

我有这个XML By URL:

<NewDataSet>
  <Table>
    <ID>1</ID>
    <SongName>AYA LIV LIVOKIM PEL?STANKTV</SongName>
    <SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song1.mp3</SongPath>
    <SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo1.png</SongImagePath>
  </Table>
  <Table>
    <ID>2</ID>
    <SongName>DîLAN PPP PELISTANK</SongName>
    <SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song2.mp3</SongPath>
    <SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo2.png</SongImagePath>
  </Table>
  <Table>
    <ID>3</ID>
    <SongName>KARIN BAL DAGRIM</SongName>
    <SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song3.mp3</SongPath>
    <SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo3.png</SongImagePath>
  </Table>
  <Table>
    <ID>4</ID>
    <SongName>RUKEN WERE CANE</SongName>
    <SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song4.mp3</SongPath>
    <SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo4.png</SongImagePath>
  </Table>
</NewDataSet>

我想在表格中显示歌曲名称,我没问题, 现在我想要当我点击桌子上的一个单元格给我歌曲路径并把它放入NSURL,因为我想用它来播放歌曲。 1 - 我在表中显示SongName没问题。 2 - 我知道如何让AVPlayer播放歌曲 只是我想点击CELL时给我一个SongPath并把它放在NSURL

这是我的Swift代码:

class ViewController: UIViewController, NSXMLParserDelegate, UITableViewDataSource, UITableViewDelegate
{

    @IBOutlet var tbData : UITableView?

    var parser = NSXMLParser()
    var posts = NSMutableArray()
    var elements = NSMutableDictionary()
    var element = NSString()
    var title1 = NSMutableString()
    var date = NSMutableString()

    override func viewDidLoad()
    {
     // Do any additional setup after loading the view, typically from a nib.
        super.viewDidLoad()
        self.beginParsing()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func beginParsing()
    {

        posts = []
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://jo.sms2tv.com/PelistankApp2/getsongs.aspx"))!)!
        parser.delegate = self
        parser.parse()

        tbData?.reloadData()
    }


    //////////////////////////////////////XMLParser Methods

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
    {
        element = elementName
        if (elementName as NSString).isEqualToString("Table")
        {
            elements = NSMutableDictionary()
            elements = [:]
            title1 = NSMutableString()
            title1 = ""
            date = NSMutableString()
            date = ""
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
    {
        if (elementName as NSString).isEqualToString("Table") {
            if !title1.isEqual(nil) {
                elements.setObject(title1, forKey: "title")
            }
            if !date.isEqual(nil) {
                elements.setObject(date, forKey: "date")
            }

            posts.addObject(elements)
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String)
    {
        if element.isEqualToString("SongName") {
            title1.appendString(string)
        } else if element.isEqualToString("SongPath") {
            date.appendString(string)
        }
    }
  ///////////////////////////////////////////XMLParser Methods


    //////////////////////////////Tableview Methods
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")!

        if(cell.isEqual(NSNull)) {
            cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil) [0] as! UITableViewCell
        }

        cell.textLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
        cell.detailTextLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String

        return cell as UITableViewCell
    }


  //////////////////////////////////////Tableview Methods


    /////// Table Action ( Cell clicked ) ///////

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        let row = indexPath.row
        print("Row: \(row)")

    }

    /////// Table Action ( Cell clicked ) ///////


    @IBAction func Song(sender: UIButton) {

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewSong")
        self.presentViewController(nextViewController, animated:true, completion:nil)

    }

   @IBAction func BackTableToHome(sender: UIBarButtonItem) {

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("Home")
        self.presentViewController(nextViewController, animated:true, completion:nil)
    }

    //////////Button SecandViewController ////
    @IBAction func SecondViewController(sender: AnyObject) {

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewSong")
        self.presentViewController(nextViewController, animated:true, completion:nil)
    }
}

这是我的表

enter image description here

对此问题的任何建议 谢谢

1 个答案:

答案 0 :(得分:0)

这是您想要的,它可以轻点打印URL。

    /////// Table Action ( Cell clicked ) ///////

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        let row = indexPath.row
        print("Row: \(row)")

        print(posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String) //Prints the song url

    }

这是项目链接 https://drive.google.com/file/d/0B2csGr9uKp1DN1VodkNndmtQR2c/view?usp=sharing