在某些情况下,Android Node.getNodeValue返回NULL

时间:2011-12-08 05:59:57

标签: java android xml

我正在尝试解析XML文件。这是文件:

<root>
        <entry>
                <username>achanda</username>
                <latitude>45.123</latitude>
                <longtitude>76.345</longtitude>
                <timestamp>2000</timestamp>
                <filename>image2.jpg</filename>
        </entry>
        <entry>
                <username>achanda</username>
                <latitude>45.123</latitude>
                <longtitude>76.345</longtitude>
                <timestamp>2000</timestamp>
                <filename>image2.jpg</filename>
        </entry>
        <entry>
                <username>achanda</username>
                <latitude>45.123</latitude>
                <longtitude>76.345</longtitude>
                <timestamp>2000</timestamp>
                <filename>image2.jpg</filename>
        </entry>
</root>

这是代码:

private String getTextValue(Element ele, String tagName) {
        String textVal = "";
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            Node n = el.getFirstChild();
            //TODO this line throws an exception for the last
            //entry in the xml file. Fix this
            textVal = n.getNodeValue();
        }
        return textVal;
    }

这会解析前两个<entry>标记的文件,但getNodeValue()会为最后一个标记返回null。我发现here在某些条件下它应该返回null。那我如何提取字段呢?我尝试将el.getFirstChild()投射到Element,即使这不起作用。

    private float getFloatValue(Element ele, String tagName) {
        return Float.parseFloat(getTextValue(ele, tagName));
    }

    public ArrayList<GeoPoint> parseFile(String path) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document dom = null;
        ArrayList<GeoPoint> gp = new ArrayList<GeoPoint>();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            dom = db.parse(path);

            org.w3c.dom.Element eroot = dom.getDocumentElement();
            NodeList nl = eroot.getElementsByTagName("entry");

            if (nl != null && nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {
                    org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(i);
                    float lat = (float) (getFloatValue(el, LAT) * 1E6);
                    float lon = (float) (getFloatValue(el, LON) * 1E6);
                    Log.e("###LAT: ", Float.toString(lat));
                    Log.e("###LON: ", Float.toString(lon));
                    //gp.add(new GeoPoint((int) lat, (int) lon));
                }
            }
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return gp;
    }

Logcat输出:

12-08 01:05:38.718: D/AndroidRuntime(3148): Shutting down VM
12-08 01:05:38.718: W/dalvikvm(3148): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-08 01:05:38.779: E/AndroidRuntime(3148): FATAL EXCEPTION: main
12-08 01:05:38.779: E/AndroidRuntime(3148): java.lang.IllegalStateException: Could not execute method of the activity
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.view.View$1.onClick(View.java:2144)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.view.View.performClick(View.java:2485)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.view.View$PerformClick.run(View.java:9080)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.os.Handler.handleCallback(Handler.java:587)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.os.Looper.loop(Looper.java:130)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invoke(Method.java:507)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at dalvik.system.NativeStart.main(Native Method)
12-08 01:05:38.779: E/AndroidRuntime(3148): Caused by: java.lang.reflect.InvocationTargetException
12-08 01:05:38.779: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at java.lang.reflect.Method.invoke(Method.java:507)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at android.view.View$1.onClick(View.java:2139)
12-08 01:05:38.779: E/AndroidRuntime(3148):     ... 11 more
12-08 01:05:38.779: E/AndroidRuntime(3148): Caused by: java.lang.NullPointerException
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.apps.terrapin.SearchActivity.getTextValue(SearchActivity.java:186)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.apps.terrapin.SearchActivity.getFloatValue(SearchActivity.java:192)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.apps.terrapin.SearchActivity.parseFile(SearchActivity.java:209)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.apps.terrapin.SearchActivity.searchData(SearchActivity.java:100)
12-08 01:05:38.779: E/AndroidRuntime(3148):     at com.apps.terrapin.SearchActivity.searchClicked(SearchActivity.java:70)
12-08 01:05:38.779: E/AndroidRuntime(3148):     ... 14 more

更新 删除xml文件的所有内容并再次写入后,此问题已得到解决。似乎Android以某种方式缓存了该文件。还有什么可能成为问题?

1 个答案:

答案 0 :(得分:0)

一旦删除了xml文件的所有内容并再次写入,此问题就解决了。似乎Android以某种方式缓存了该文件。