提问人:Ega Setya Putra 提问时间:8/6/2015 更新时间:8/6/2015 访问量:1369
如何从XML中提取链接href?
How to extract link href from XML?
问:
我正在为我的应用程序使用AEXML,每当我尝试从xml数据中提取链接时,我都会遇到麻烦。
下面是 XML 数据:
<entry>
<updated>2015-08-06T01:17:16-07:00</updated>
<id im:id="969299396">https://itunes.apple.com/us/album/love-and-marriage/id969298500?i=969299396&uo=2</id>
<title>Love and Marriage - Frank Sinatra</title>
<im:name>Love and Marriage</im:name>
<link rel="alternate" type="text/html" href="https://itunes.apple.com/us/album/love-and-marriage/id969298500?i=969299396&uo=2" />
<im:contentType label="Music" term="Music">
<im:contentType label="Track" term="Track" />
</im:contentType>
<category term="Easy Listening" label="Easy Listening" im:id="25" scheme="https://itunes.apple.com/us/genre/music-easy-listening/id25?uo=2" />
<link href="http://a1901.phobos.apple.com/us/r1000/020/Music5/v4/c1/9c/ed/c19ced58-1bd8-4940-7edd-a3c540be7584/mzaf_5226977055945471954.plus.aac.p.m4a" title="Preview" rel="enclosure" type="audio/x-m4a" im:assetType="preview">
<im:duration>30000</im:duration>
</link>
<im:artist href="https://itunes.apple.com/us/artist/frank-sinatra/id171366?uo=2">Frank Sinatra</im:artist>
<im:price amount="1.29000" currency="USD">$1.29</im:price>
<im:image height="55">http://is5.mzstatic.com/image/pf/us/r30/Music3/v4/48/8c/6c/488c6cd9-f2e2-fb72-a1d9-f8e0a6cbce63/UMG_cvrart_00602547253309_01_RGB72_1500x1500_15UMGIM08561.55x55-70.jpg</im:image>
<im:image height="60">http://is2.mzstatic.com/image/pf/us/r30/Music3/v4/48/8c/6c/488c6cd9-f2e2-fb72-a1d9-f8e0a6cbce63/UMG_cvrart_00602547253309_01_RGB72_1500x1500_15UMGIM08561.60x60-50.jpg</im:image>
<im:image height="170">http://is1.mzstatic.com/image/pf/us/r30/Music3/v4/48/8c/6c/488c6cd9-f2e2-fb72-a1d9-f8e0a6cbce63/UMG_cvrart_00602547253309_01_RGB72_1500x1500_15UMGIM08561.170x170-75.jpg</im:image>
<rights>℗ 2015 Universal Music Enterprises</rights>
<im:releaseDate label="April 21, 2015">2015-04-21T00:00:00-07:00</im:releaseDate>
<im:collection>
<im:name>Ultimate Sinatra</im:name>
<link rel="alternate" type="text/html" href="https://itunes.apple.com/us/album/ultimate-sinatra/id969298500?uo=2" />
<im:contentType label="Music" term="Music">
<im:contentType label="Album" term="Album" />
</im:contentType>
</im:collection>
<content type="html"><table border="0" width="100%">
<tr>
<td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td align="center" width="166" valign="top">
<a href="https://itunes.apple.com/us/album/love-and-marriage/id969298500?i=969299396&uo=2"><img border="0" alt="Frank Sinatra - Love and Marriage artwork" src="http://is1.mzstatic.com/image/pf/us/r30/Music3/v4/48/8c/6c/488c6cd9-f2e2-fb72-a1d9-f8e0a6cbce63/UMG_cvrart_00602547253309_01_RGB72_1500x1500_15UMGIM08561.170x170-75.jpg" /></a>
</td>
<td width="10"><img alt="" width="10" height="1" src="https://s.mzstatic.com/images/spacer.gif" /></td>
<td width="95%">
<b><a href="https://itunes.apple.com/us/album/love-and-marriage/id969298500?i=969299396&uo=2">Love and Marriage</a></b><br/>
<a href="https://itunes.apple.com/us/album/ultimate-sinatra/id969298500?uo=2">Ultimate Sinatra</a><br/>
<a href="https://itunes.apple.com/us/artist/frank-sinatra/id171366?uo=2">Frank Sinatra</a>
<font size="2" face="Helvetica,Arial,Geneva,Swiss,SunSans-Regular">
<br/>
<b>Genre:</b> <a href="https://itunes.apple.com/us/genre/music-easy-listening/id25?uo=2">Easy Listening</a>
<br/>
<b>Price:</b> $1.29
<br/>
<b>Release Date:</b> April 21, 2015
</font>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<font size="2" face="Helvetica,Arial,Geneva,Swiss,SunSans-Regular"> © ℗ 2015 Universal Music Enterprises</font>
</td>
</tr>
这是我尝试过的代码:
var error: NSError?
var data : NSData = NSData(contentsOfURL: NSURL(string: "https://itunes.apple.com/us/rss/topsongs/limit=10/genre=25/xml")!)!
if let xmlDoc = AEXMLDocument(xmlData: data, error: &error) {
println(xmlDoc.xmlString)
println("rene 2")
if let cats = xmlDoc.root["entry"].all {
println(cats)
println("rene 3")
for cat in cats {
println("rene 4")
var title = cat["title"].value
var artist = cat["artist"].value
var albumUrl = cat["im:image"].value
var tes = cat.attributes["link"]!.stringValue
println(">>>>>\(tes)<<<<<<")
}
}
}
我打印标题、艺术家等没有问题。但是当我尝试获取/提取链接 href 时,我总是失败。
请给我一个建议。
答:
1赞
John Tracid
8/6/2015
#1
如果要从元素中获取属性,则需要先获取元素:href
link
for link in cat["link"].all! {
if let href = link.attributes["href"] as? String {
}
}
评论
0赞
Ega Setya Putra
8/7/2015
如何只获取第二个链接元素?
0赞
John Tracid
8/7/2015
函数返回数组。因此,您可以按索引对数组中的元素进行寻址。all
评论