gpt4 book ai didi

java - 解析 XML 无法通过重复标题正确加载内容

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:24 26 4
gpt4 key购买 nike

我正在创建 XML Reader 应用程序,在其中尝试解析 XDA-Developers 站点提要中的内容。问题是它不断重复第一行,而且还给了我一些错误。

我正在使用这个sample 。示例中的链接工作正常,但是当我放置链接时,有时会出现错误。

我的 XMLParser:

public class XMLParserXDA extends DefaultHandler{

private String URL_MAIN = "http://www.xda-developers.com/feed/?format=xml";
String TAG = "XMLHelper";

Boolean currTag = false;
String currTagVal = "";
private XDAObject post = null;
private ArrayList<XDAObject> posts = new ArrayList<>();

public ArrayList<XDAObject> getPostsList() {
return this.posts;
}

public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;

if (localName.equalsIgnoreCase("title")) {
post.setTitle(currTagVal);
}

else if (localName.equalsIgnoreCase("description")) {
post.setDescription(currTagVal);
}

else if (localName.equalsIgnoreCase("guid")) {
post.setGuid(currTagVal);
}

else if (localName.equalsIgnoreCase("item")) {
posts.add(post);
}
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "START TAG: " + localName);

currTag = true;
currTagVal = "";
if (localName.equals("item"))
post = new XDAObject();
}

}

问题出在这里:

01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: rss
01-13 20:22:08.049 4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: channel
01-13 20:22:08.049 4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: title
01-13 20:22:08.052 4274-4294/com.kostas.myapplication E/XMLHelper﹕ Exception: Attempt to invoke virtual method 'void activityobjects.XDAObject.setTitle(java.lang.String)' on a null object reference
01-13 20:22:09.103 4274-4289/com.kostas.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-13 20:22:09.103 4274-4289/com.kostas.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa36c8e80, error=EGL_SUCCESS
01-13 20:22:09.180 4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 102 frames! The application may be doing too much work on its main thread.
01-13 20:22:12.469 4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 197 frames! The application may be doing too much work on its main thread.

XDAObject 类是:

public class XDAObject {
private String title = null;
private String guid = "";
private String description = "";
private String content = "";


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getGuid() {
return guid;
}

public void setGuid(String link) {
this.guid = link;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

有什么想法吗?提前致谢!!

最佳答案

currtagval = ""= Nothing 作为字符串,并使用参数 no 覆盖方法字符

所以 currtagval = currtagval + 字符(intchar[] ch, int start, int length)

=

currtagvall = ""+ 未使用的方法 ==> null?

关于java - 解析 XML 无法通过重复标题正确加载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930909/

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