【问题标题】:Parse XML stored in internal memory解析存储在内存中的 XML
【发布时间】:2011-11-15 16:51:14
【问题描述】:

我在内部存储中创建了一个名为“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);
 }

【问题讨论】:

    标签: android xml


    【解决方案1】:

    代码是正确的。经过大量调试,当第一行仍然不是 XML 行时,我看到它崩溃了。只要您关心只传递 XML 行,代码就可以工作。我会接受答案,以便需要代码的人得到它,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多