简体   繁体   中英

Parsing XML to TextView: android

I am working on an app that displays a list of books in android. Where I retrieve an XML file from a server and parsing the contents to corresponding TextViews.

my XML file:

<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>

I am using a ViewModel to parse the XML onto my app using a NodeList.

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;
        }

I get this error during runtime:

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

book.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;

}

I would like to be able to get the child nodes of the author. I would appreciate it if anyone could please offer any advice as to what I am doing wrong?

Thanks

The error you are receiving is very clear:

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

Meaning, you are trying to invoke the getFirstChildMethod on an object which is null.

I would check that in the line below:

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

title has indeed a value.

Without seeing the actual XML you are parsing, these lines are the culprit:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM