在XML Parser中应用DOM技术时得到NullPointerException

时间:2015-07-31 23:37:47

标签: java android xml dom

我运行了这个程序,它在此行报告了NullPointerException

NodeList nodeList = root.getChildNodes();

以下是整个事情:

    package com.example.dom_technique;

    import java.io.IOException;
    import java.io.InputStream;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class DOM extends Activity {

    private Button button;
    private TextView textView;

    protected void onCreate(Bundle x) {
        super.onCreate(x);
        setContentView(R.layout.activity_dom);

        button = (Button) findViewById(R.id.button1);
        textView = (TextView) findViewById(R.id.textView1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    readXML();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        });
    }

    private void readXML() throws ParserConfigurationException, SAXException, IOException {
        String output = "";

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        InputStream inputStream = getResources().openRawResource(R.drawable.student);
        Document doc = builder.parse(inputStream);

        Element root = doc.getDocumentElement();
        NodeList nodeList = root.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            if (node instanceof Element) {
                Element student = (Element) node;
                String name = student.getAttribute("name");
                String student_id = student.getAttribute("student_id");

                NodeList subNodeList = student.getElementsByTagName("school");  
                String school = subNodeList.item(0).getTextContent();

                subNodeList = student.getElementsByTagName("major");
                String major = subNodeList.item(0).getTextContent();

                output += name + "  " + student_id + "\n" + school + "\n" + major +"\n" + "     ***********" + "\n";
            }
        }
        textView.setText(output);
    }

}

我自己写的XML文件:

<?xml version="1.0" encoding="utf-8"?><students>

<student name="Henry Thompson" student_id="63827614">

    <school>University of California, Berkeley</school>
    <major>Computer Science</major>

</student>

<student name="Sheila Lopez" student_id="02817281">

    <school>University of California, Los Angeles</school>
    <major>Electrical Engineering</major>

</student>

<student name="Gabriel Carter" student_id="92717290">

    <school>University of California, Davis</school>
    <major>Mathematics</major>

</student>

<student name="Stephanie Nguyen" student_id="83729337">

    <school>University of California, San Diego</school>
    <major>Business Administraion, Concentration in Marketing</major>

</student>

</students>

我几乎无法理解为什么,我以为我经历了各种要求。我怀疑问题可能来自XML文件。起初,我尝试在drawable文件夹中创建一个XML文件并报告错误。因此我在记事本中写道,用xml扩展名保存并导入drawable,然后就可以了。 请帮我!!!非常感谢!!!!!!

2 个答案:

答案 0 :(得分:0)

看起来您的xml最终缺少结束</students>标记。因此无法正确解析根元素(即“学生”)。

开头的“学生”标签有点隐藏在第一行。

答案 1 :(得分:0)

对于那些花时间考虑我的问题的人,我想对所有人表示感谢 幸运的是,我想出了如何修复我的,这只是将student.xml重命名为student。似乎我们导入的所有内容都是可绘制的,如果我们希望它能够工作,我们就不应该提到扩展。我试过,它运作得很好。 再一次,非常感谢!!!!