我试图为播客制作应用。我的最终目标是让用户能够点击列表中的一集(由RSS提要生成),然后进行音频播放(也可以从RSS提要中检索)我的问题是解析XML来自RSS提要。我似乎无法弄清楚如何从mp3的机箱标签中取出URL。任何帮助,将不胜感激。这是我的解析类
private String data;
private ArrayList<Episodes> mEpisodes;
public ParseEpisodes(String xmlData) {
this.data = xmlData;
mEpisodes = new ArrayList<>();
}
public ArrayList<Episodes> getEpisodes() {
return mEpisodes;
}
public boolean process(){
boolean status = true;
Episodes currentRecord = null;
boolean inEntry = false;
String textValue = "";
String urlValue = "";
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(this.data));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
switch(eventType){
case XmlPullParser.START_TAG:
//Log.d("ParseEpisodes", "Starting tag for " + tagName);
if(tagName.equalsIgnoreCase("item")){
inEntry = true;
currentRecord = new Episodes();
}
break;
case XmlPullParser.TEXT:
textValue = xpp.getText();
urlValue = xpp.getText();
break;
case XmlPullParser.END_TAG:
//Log.d("ParseEpisodes", "Ending tag for " + tagName);
if(inEntry){
if(tagName.equalsIgnoreCase("item")){
mEpisodes.add(currentRecord);
inEntry = false;
}else if(tagName.equalsIgnoreCase("title")){
currentRecord.setTitle(textValue);
}else if(tagName.equalsIgnoreCase("enclosure")){
currentRecord.setLink(urlValue);
}
}
break;
}
eventType = xpp.next();
}
}catch (Exception e){
status = false;
e.printStackTrace();
}
return true;
}
这是xml
<item>
<title>Episode 52 - Facebook Nukes The Show</title>
<link>https://greynoi.se/podcasts/episode-52-facebook-nukes-the-show</link>
<enclosure url="http://greynoi.se/episodes/ep79_m.mp3" length="43312404" type="audio/mpeg"/>
<pubDate>Fri, 15 Jul 2016 15:22:24 -0700</pubDate>
<category>1</category>
<source url="https://greynoi.se">The GR3YNOISE Podcast</source>
<itunes:subtitle>Episode 52 - Facebook Nukes The Show</itunes:subtitle>
<itunes:summary>Episode 52 - Facebook Nukes The Show</itunes:summary>
<content:encoded><![CDATA[Episode 52 - Facebook Nukes The Show"}]]></content:encoded>
<guid>https://greynoi.se/podcasts/episode-52-facebook-nukes-the-show</guid>
</item>
答案 0 :(得分:0)
所以你想要在封装url = tag之后的url?
如果是这样的话很容易。只需使用子字符串来查找并获取网址。
这样的事情应该有效。
<-- HOME -->
{% include "navbar_template.html" with page="home"} %}
###Do something with {{page}} variable in order to set the home list tag to active.
答案 1 :(得分:0)
试试这个:
...
YOUR CODE
...
while(eventType != XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
if (tagName.equalsIgnoreCase("enclosure")) {
int attributeCount = xpp.getAttributeCount();
for(int i = 0; i<attributeCount; i++){
String attrib = xpp.getAttributeName(i);
if(attrib && attrib.equalsIgnoreCase("url")){
String url = xpp.getAttributeValue(i);
}
}
}
if (url != null && !url.isEmpty() && !url.equals("null")) {
Log.d("ParseEpisodes", "URL to mp3: " + url);
}
...
THE REST OF YOUR CODE
...
}
答案 2 :(得分:0)
解决方案最终非常简单。我需要做的只是删除
urlValue = xpp.getText();
来自
case XmlPullParser.TEXT:
然后改变
else if(tagName.equalsIgnoreCase("enclosure")){
urlValue = xpp.getAttributeValue(0);
currentRecord.setLink(urlValue);
}
中的
case XmlPullParser.END_TAG: