如何从两个URL解析两个XML文件(Swift) [英] How can I parse two XML files from two URLs (Swift)
问题描述
我有两个XML网址。第一个URL在表
上显示 SongName
,第二个URL显示 SongPath
。当我点击表格中的 SongName
时,它会转到第二个网址中的 SongPath
。这是
表。您可以在代码中看到 SongName
URL。
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.
这是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>
这是 SongPath :
这就是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>
这是我的Swift代码:
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)
}
}
任何建议请。我可以制作MP3播放器没问题,我只是想要它,这样当我点击表格中的一个单元格时,它将在第二个XML URL中的同一行播放该歌曲。
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.
推荐答案
首先解析您的两个API并将歌曲名称和歌曲URL存储在一个数组中。并且无论何时不想再次解析,都可以访问它。
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.
这个答案是使用 SWXMLHash
您可以使用 Alamofire 发出HTTP请求
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
}
有关XML解析的更多信息,我已经已回答
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
}
这是项目链接
https://drive.google.com / file / d / 0B2csGr9uKp1DN1VodkNndmtQR2c / view?usp = sharing
这篇关于如何从两个URL解析两个XML文件(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!