gpt4 book ai didi

java - 将XML解析为TextView : android

转载 作者:行者123 更新时间:2023-12-01 17:45:54 25 4
gpt4 key购买 nike

我正在开发一个在 Android 中显示图书列表的应用程序。我从服务器检索 XML 文件并将内容解析为相应的 TextView。

我的 XML 文件:

<bib>
<book year="1988">
<title>Book Title</title>
<author>
<last>Jones</last>
<first>Ryan</first>
</author>
</book>

<book year="2001">
<title>Book Title 2</title>
<author>
<last>Ryans</last>
<first>Jack</first>
</author>
</book>

我正在使用 ViewModel 通过 NodeList 将 XML 解析到我的应用程序中。

ViewModel.java:

try {

String bFeed = getApplication().getString(R.string.feed);

URL url = new URL(bFeed);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

// Parse the book feed.
Document dom = db.parse(in);
// Returns the root element.
Element docEle = dom.getDocumentElement();
books.clear();

NodeList nl = docEle.getElementsByTagName("book");
if (nl != null && nl.getLength() > 0) {

for (int i = 0; i < nl.getLength(); i++) {

if (isCancelled()) {
return books;
}
Element bookElement = (Element) nl.item(i);
Element title = (Element) bookElement
.getElementsByTagName("title").item(0);
Element author = (Element) bookElement
.getElementsByTagName("author").item(0);

String bookTitle = title.getFirstChild().getNodeValue();
String bookYear = ((Element) bookElement).getAttribute("year");
// Error occurs
String bookAuthor = author.getFirstChild().getNodeValue();

final Book bookObject = new Book(bookTitle, bookYear, bookAuthor);
books.add(bookObject);

}
}
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
} catch (ParserConfigurationException e) {
Log.e(TAG, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.e(TAG, "SAX Exception", e);
}

return books;
}

我在运行时遇到此错误:

原因:java.lang.NullPointerException:尝试在空对象引用上调用接口(interface)方法“org.w3c.dom.Node org.w3c.dom.Element.getFirstChild()”

书.java:

public class Book {
private String year;
private String title;
private String author;


public String getYear() { return year; }

public String getTitle() {
return title;
}

public String getAuthor() { return author; }

public Book(String year, String title, String author) {

this.year = year;
this.title = title;
this.author = author;

}

我希望能够获取作者的子节点。如果有人能就我做错了什么提供任何建议,我将不胜感激?

谢谢

最佳答案

您收到的错误非常清楚:

java.lang.NullPointerException: Attempt to invoke interface method 'org.w3c.dom.Node org.w3c.dom.Element.getFirstChild()' on a null object reference

意思是,您试图在一个空对象上调用 getFirstChildMethod。

我会在下面的行中检查:

String bookTitle = title.getFirstChild().getNodeValue();

标题确实有值(value)。

如果没有看到您正在解析的实际 XML,这些行就是罪魁祸首:

Element bookElement = (Element) nl.item(i);
Element title = (Element) bookElement.getElementsByTagName("title").item(0);

关于java - 将XML解析为TextView : android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60866588/

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