我如何获得NodeList的结果?

时间:2012-03-16 12:56:24

标签: android xmlnode

我想在文本视图中显示Node的值,但是我无法获取Node的值。我有一个原始的xml文件,名称为country,节点标记为name,alpha2和diallingcode。 以下是我的代码

public class NewTester extends Activity {
/** Called when the activity is first created. */

TextView display;
String getinput;
String data,result;
int code;
InputStream instream;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    display=(TextView)findViewById(R.id.tvdisplay);

    String dphoneNumber="2345678";

    data=dphoneNumber.substring(0,3);



    try {
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        instream = getResources().openRawResource(R.raw.mycountries);
        Document doc=db.parse(instream,null);

        doc.getDocumentElement().normalize();

        NodeList nodeList=doc.getElementsByTagName("country");

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

            Element ele=(Element)node;
            NodeList Countryname=ele.getElementsByTagName("name");
            NodeList CountryAlpha=ele.getElementsByTagName("alpha2");
            NodeList Countrydial=ele.getElementsByTagName("diallingcode");
            if(Countrydial.item(i).getNodeValue()==data){
                result=CountryAlpha.item(i).getNodeValue();

            }

        }

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    display.setText(""+result);
}
}

我收到此错误

 03-16 16:19:02.478: E/AndroidRuntime(369): FATAL EXCEPTION: main
03-16 16:19:02.478: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.tester/com.tester.NewTester}: java.lang.NullPointerException

03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)      03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.os.Handler.dispatchMessage(Handler.java:99)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.os.Looper.loop(Looper.java:123)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread.main(ActivityThread.java:4627)    03-16 16:19:02.478:E / AndroidRuntime(369):at java.lang.reflect.Method.invokeNative(Native Method)    03-16 16:19:02.478:E / AndroidRuntime(369):at java.lang.reflect.Method.invoke(Method.java:521)    03-16 16:19:02.478:E / AndroidRuntime(369):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)    03-16 16:19:02.478:E / AndroidRuntime(369):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)    03-16 16:19:02.478:E / AndroidRuntime(369):at dalvik.system.NativeStart.main(Native Method)    03-16 16:19:02.478:E / AndroidRuntime(369):引起:java.lang.NullPointerException    03-16 16:19:02.478:E / AndroidRuntime(369):at com.tester.NewTester.onCreate(NewTester.java:61)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)    03-16 16:19:02.478:E / AndroidRuntime(369):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)     03-16 16:19:02.478:E / AndroidRuntime(369):... 11 more

1 个答案:

答案 0 :(得分:3)

替换代码,

    Node node=nodeList.item(i);
    Element ele=(Element)node;

    Element ele=(Element)nodeList.item(i);