Ahmad Mahamid Ahmad Mahamid - 6 months ago 63
Swift Question

How can I parse two XML files from two URLs (Swift)

I have two XML URLs. The first URL shows a

SongName
on the
Table
and the second URL has the
SongPath
. When I click the
SongName
on the table it goes to the
SongPath
in the second URL. This is the
table. You can see the
SongName
URL in the code.

This the XML:

<NewDataSet>
<Table>
<SongName>AYA LIV LIVOKIM PEL?STANKTV</SongName>
</Table>
<Table>
<SongName>DîLAN PPP PELISTANK</SongName>
</Table>
<Table>
<SongName>KARIN BAL DAGRIM</SongName>
</Table>
<Table>
<SongName>RUKEN WERE CANE</SongName>
</Table>
</NewDataSet>


This the second URL for the SongPath :

And this the XML :

<NewDataSet>
<Table>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song1.mp3</SongPath>
</Table>
<Table>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song2.mp3</SongPath>
</Table>
<Table>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song3.mp3</SongPath>
</Table>
<Table>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song4.mp3</SongPath>
</Table>
</NewDataSet>


And this my Swift code:

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/PelistankApp/default.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("pubDate") {
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)
}
}


Any advice please. I can make an MP3 player no problem, I just want it so that when I click on a cell in the table it will play the song in the same row in the second XML URL.

Answer

First parse your both API and store songs name and songs URL in an array. and access it whenever you want don't parse again and again.

This answer is parsing using SWXMLHash

you can use Alamofire to make HTTP request

let xml = SWXMLHash.parse(data!)
       for elem in xml["NewDataSet"]["Table"] {

            print(elem["SongName"].element?.text) //PRINT SONG NAME 
        }

For more about XML Parsing I have already answered

//Here is what you wanted

    /////// 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

    }

Here is the project link https://drive.google.com/file/d/0B2csGr9uKp1DN1VodkNndmtQR2c/view?usp=sharing