2015-10-15 120 views
0

我做了一個字符串,其中包含我正在處理的xml的一部分。 該文件中有一些整數,我無法將它們從String轉換爲int。我嘗試​​但它沒有工作。所以我想知道如果有人可能有解決我的問題。由於XML解析將字符串轉換爲int

import android.os.AsyncTask; 
import android.util.Log; 

import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserFactory; 


import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringReader; 
import java.net.URL; 



class ReadXMLFile extends AsyncTask<Void, Void, Boolean> { 


    @Override 
    protected Boolean doInBackground(Void... params) { 
     Log.d("beginning: ", "Pocetak"); 

     //TODO - Call getFeeds Method to populate feeds list & return true/false depending on result of operation 
     try { 

      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(true); 
      XmlPullParser xpp = factory.newPullParser(); 

      String xmldata = <cenovnik> 
<cenovnikStavkaList> 
    <id>12195432</id> 
    <izlaznaStanica>100</izlaznaStanica> 
    <kategorijaVozila>0</kategorijaVozila> 
    <ulaznaStanica>100</ulaznaStanica> 
</cenovnikStavkaList> 
<cenovnikStavkaList> 
    <id>12197782</id> 
    <izlaznaStanica>100</izlaznaStanica> 
    <kategorijaVozila>1</kategorijaVozila> 
    <ulaznaStanica>100</ulaznaStanica> 
</cenovnikStavkaList> 

   xpp.setInput(new StringReader(xmldata)); 
      Log.d("step1a", "string"); 
      int eventType = xpp.getEventType(); 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       String tagname = xpp.getName(); 
       if(eventType == XmlPullParser.START_DOCUMENT) { 
        Log.d("Step 2a: ","Start document"); 
       } else if(eventType == XmlPullParser.START_TAG) { 
        Log.d("Step 2b: ", "Start tag " + xpp.getName()); 
        if(xpp.getName().equals("cenovnikStavkaList")){ 

         String testid = xpp.getAttributeValue(null, "id"); 
         Log.d("Step 2d - ID: ", testid); 
        } 


       } else if(eventType == XmlPullParser.END_TAG) { 

       } else if(eventType == XmlPullParser.TEXT) { 

        Log.d("Step 2d: ", xpp.getText()); 

       } 

       eventType = xpp.next(); 
      } 
      Log.d("Step 2e: ", "End document"); 
      Log.d("Step 2: ", "radi"); 

      return true; 
     } catch (Exception e) { 
      return false; 
     } 

    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     if(result){ 
      Log.d("END TEST","done"); 
     } 
    } 

    public static InputStream getInputStream(URL url) { 
     try { 
      return url.openConnection().getInputStream(); 
     } catch (IOException e) { 
      return null; 
     } 
    } 



} 
+0

你似乎沒有屬性(有或沒有整數值)。您正在尋找將文本值從「String」轉換爲整數類型的元素。 – Mena

回答

0

的數字是葉標籤裏面,像<id>12195432</id>

XML解析事件需要狀態,將數據保存在變量中。 正式你可以有幾個文本兩個標籤裏面,因此:

StringBuilder text; 

      } else if(eventType == XmlPullParser.START_TAG) { 
       text = new StringBuilder(); 
      } else if(eventType == XmlPullParser.TEXT) { 
       if (text != null) { 
        text.append(xpp.getText()); 
       } 
      } else if(eventType == XmlPullParser.END_TAG) { 
       if (text != null) { 
        String s = text.toString(); 
        try { 
         int n = Integer.parseInt(s); 
         // do something with number n 
        } catch (NumberFormatException e) { 
         // No number 
        } 
        text = null; 
       } 
      } 

與自己創建一個新的領域cenovnikStavkaList每一個同名的標籤字段ID,並做

cenovnikStavkaList.id = n; 

和END_TAG添加它到一個列表。