gpt4 book ai didi

Android - 无法解析 XML 中的附件标签

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:10 25 4
gpt4 key购买 nike

我正在使用以下代码来解析 XML 文件。一切正常,除非我尝试从附件标签读取 url 属性。我读过的所有内容都显示使用 attributes.getValue("url") 但我似乎仍然得到一个空值。也许我没有正确返回它?

我试图解析的具体行是这样的:

这是我尝试使用按钮播放 mp3 时在 LogCat 中看到的唯一信息。我/StagefrightPlayer(33):setDataSource('')E/MediaPlayer(275):在状态 4 中开始调用E/MediaPlayer( 275): 错误 (-38, 0)E/MediaPlayer(275):错误(-38,0)

我认为导致问题的行在底部: else if (localName.equalsIgnoreCase(ENC)) { String link = attributes.getValue("url"); currentMessage.setEnc(link);

谢谢。

package com.Demo;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import static com.Demo.BaseFeedParser.*;

public class RssHandler extends DefaultHandler{
private List<Message> messages;
private Message currentMessage;
private StringBuilder builder;


public List<Message> getMessages(){
return this.messages;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
builder.append(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
super.endElement(uri, localName, name);
if (this.currentMessage != null){
if (localName.equalsIgnoreCase(TITLE)){
currentMessage.setTitle(builder.toString());
} else if (localName.equalsIgnoreCase(LINK)){
currentMessage.setLink(builder.toString());
} else if (localName.equalsIgnoreCase(DESCRIPTION)){
currentMessage.setDescription(builder.toString());
} else if (localName.equalsIgnoreCase(PUB_DATE)){
currentMessage.setDate(builder.toString());
} else if (localName.equalsIgnoreCase(SUMMARY)) {
currentMessage.setSummary(builder.toString());
} else if (localName.equalsIgnoreCase(ITEM)){
messages.add(currentMessage);
builder.setLength(0);
}
}
}

@Override
public void startDocument() throws SAXException {
super.startDocument();
messages = new ArrayList<Message>();
builder = new StringBuilder();
}

@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if (localName.equalsIgnoreCase(ITEM)){
this.currentMessage = new Message();
}
else if (localName.equalsIgnoreCase(ENC)) {
String link = attributes.getValue("url");
currentMessage.setEnc(link);
}
}
}

最佳答案

这个电话

       String link = attributes.getValue("url");

需要参数是 XML 限定(前缀)名称,而“url”可能只是本地名称。

要对此进行调试,您可以使用 getLength() 查看所有属性以确定有多少,然后使用 getLocalName(int index) getQName(int index) 显示名称的不同形式。

关于Android - 无法解析 XML 中的附件标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8143268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com