解析存储在内部存储器中的XML

时间:2011-11-15 16:51:14

标签: android xml

我在内部存储中创建了一个名为“temp.gpx”的XML文件,现在我想解析它。我想用这种方法来解析并获得GPS坐标和其他一些东西。检测到DOCUMENT_START(其Log.d行被写入),但后来我得到一个异常,没有任何关于哪条确切行导致它的线索。

例外是“无法添加窗口:令牌null不适用于应用程序”。永远不会写入Log.d(TAG,“next”)。

    private void procesarGPX() throws XmlPullParserException, IOException {

   String tag = new String();
   float lat, lon;
   trackData = new TrackData(true, true);

   FileInputStream leerFichero = getApplicationContext().openFileInput("temp.gpx");

   XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
   factory.setNamespaceAware(true);
   XmlPullParser xpp = factory.newPullParser();
   xpp.setInput(leerFichero, null);

   int eventType = xpp.getEventType();
   while (eventType != XmlPullParser.END_DOCUMENT){
       if(eventType == XmlPullParser.START_DOCUMENT){
           Log.d(TAG, "START_DOC");
       }
       else if(eventType == XmlPullParser.START_TAG){
           Log.d(TAG, "START_TAG");
            tag = xpp.getName();
            if(tag.equals("name")) boolName = true;
            else if(tag.equals("trkpt")){
                lat = Float.parseFloat(xpp.getAttributeValue(null, "lat"));
                lon = Float.parseFloat(xpp.getAttributeValue(null, "lon"));
                if(lat*(-1)<=180 && lon*(-1)<=180) trackData.addPoint(new GeoPoint((int)(lat*1E6), (int)(lon*1E6)));
            }
            else if(tag.equals("ele")) boolEle = true;
            else if(tag.equals("time")) boolTime = true;
            else if(tag.equals("gpx")){
                Log.d(TAG, "START_TAG es del tipo gpx");
                trackData.setAutor(xpp.getAttributeValue(null, "creator"));
                trackData.setVersion(xpp.getAttributeValue(null, "version"));
            }
        }
        else if(eventType == XmlPullParser.END_TAG){
            Log.d(TAG, "END_TAG");
            if(tag.equals("name")) boolName = false;
            else if(tag.equals("ele")) boolEle = false;
            else if(tag.equals("time")) boolTime = false;
        }
        else if(eventType == XmlPullParser.TEXT){
            Log.d(TAG, "TEXT");
            if(boolName) trackData.setName(xpp.getText());
            else if(boolEle) trackData.addElevationValue(Float.parseFloat(xpp.getText()));
            else if(boolTime) parseTime(xpp.getText());
        }
        eventType = xpp.next();Log.d(TAG, "next()");
   }

   Log.d(TAG, "GPX processed");

   Intent mapaIntent = new Intent(this, pfc.uniovi.MapaActivity.class);
   startActivity(mapaIntent);
 }

1 个答案:

答案 0 :(得分:0)

代码是对的。经过大量的调试后,当第一行仍不是XML行时,我看到它崩溃了。只要您只想传递XML行,代码就可以工作。我会接受答案,所以任何需要代码的人都可以获得,