2014-06-11 18 views
0

我試圖將this xml file加載到Android應用程序中。我得到了一切工作,但由於某種原因,我無法加載id。 我一直在谷歌搜索,發現我應該使用parser.getattribute(null,"id"); ,但由於某種原因,它不斷返回null。我也嘗試過parser.getattribute(0);parser.getattribute(3);,它給了我一個越界異常,但是當我通過程序進行調試並查看解析器類時,值始終包含idAndroid XML解析器無法獲取屬性

這是一個簡單的修復,但有沒有人有我如何訪問每個數據集的ID的想法? 我沒有問題,得到的volumeavgmaxminstddecmedianpercentile的值,但我需要的id也。

這裏是我的XML文件閱讀器類:

public class EveCentralRead { 

    static final String KEY_SITE = "type"; 


    static final String KEY_volume ="volume"; 
    static final String KEY_avg = "avg"; 
    static final String KEY_max ="max"; 
    static final String KEY_min ="min"; 
    static final String KEY_stddev = "stddev"; 
    static final String KEY_median = "median"; 
    static final String KEY_precentile ="percentile"; 
    static final String KEY_typeID = "type"; 

    public static List<EveCentralHolder> getEveCentralDataFromXML(Context ctx) { 

     // List of StackSites that we will return 
     List<EveCentralHolder> stackSites; 
     stackSites = new ArrayList<EveCentralHolder>(); 

     // temp holder for current StackSite while parsing 
     EveCentralHolder curStackSite = null; 
     // temp holder for current text value while parsing 
     String curText = ""; 

     try { 
      // Get our factory and PullParser 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      XmlPullParser xpp = factory.newPullParser(); 

      // Open up InputStream and Reader of our file. 
      File file = new File(MainActivity.MainDir+"/prices.xml"); 


      InputStream fis = new FileInputStream(file.getPath()); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 

      // point the parser to our file. 
      xpp.setInput(reader); 

      // get initial eventType 
      int eventType = xpp.getEventType(); 

      // Loop through pull events until we reach END_DOCUMENT 
      boolean getdata = false; 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       // Get the current tag 
       String tagname = xpp.getName(); 

       // React to different event types appropriately 
       switch (eventType) { 
        case XmlPullParser.START_TAG: 
//      Log.d("hej",tagname); 
         if(tagname.equalsIgnoreCase(KEY_typeID)){ 
          curStackSite = new EveCentralHolder(); 
          curStackSite.setTypeID(xpp.getAttributeValue(null,"id")); 

         } 
         if(tagname.equalsIgnoreCase("sell")){ 
          getdata = true; 
          curStackSite = new EveCentralHolder(); 
         } 

         break; 

        case XmlPullParser.TEXT: 
         curText = xpp.getText(); 
         if(getdata) { 
          curText = xpp.getText(); 
         // Log.d("hej", "" + xpp.getText()); 
         } 
         break; 

        case XmlPullParser.END_TAG: 
//      Log.d("hej",tagname); 
         if (tagname.equalsIgnoreCase("sell")) { 
          getdata = false; 

         } 
         if (tagname.equalsIgnoreCase(KEY_SITE)) { 

          stackSites.add(curStackSite); 
         } 
         if(getdata){ 
          if (tagname.equalsIgnoreCase(KEY_volume)) { 

           curStackSite.setTypeID(xpp.getAttributeValue(null,"id")); 
           curStackSite.setVolume(curText); 
          } else if (tagname.equalsIgnoreCase(KEY_avg)) { 

           curStackSite.setAvg(curText); 
          } else if (tagname.equalsIgnoreCase(KEY_max)) { 

           curStackSite.setMax(curText); 
          } else if (tagname.equalsIgnoreCase(KEY_min)) { 

           curStackSite.setMin(curText); 
          }else if (tagname.equalsIgnoreCase(KEY_stddev)) { 

           curStackSite.setStddev(curText); 
          }else if (tagname.equalsIgnoreCase(KEY_precentile)) { 

           curStackSite.setPercentile(curText); 
          } 
         } 


         break; 

        default: 
         break; 
       } 
       //move on to next iteration 
       eventType = xpp.next(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // return the populated list. 
     return stackSites; 
    } 
} 

這裏是我的XML文件:

<evec_api version="2.0" method="marketstat_xml"> 
<marketstat> 
<type id="34"> 
<buy> 
<volume>46278278835</volume> 
<avg>6.02</avg> 
<max>6.44</max> 
<min>2.96</min> 
<stddev>0.62</stddev> 
<median>6.22</median> 
<percentile>6.43</percentile> 
</buy> 
<sell> 
<volume>12569824799</volume> 
<avg>6.64</avg> 
<max>8.85</max> 
<min>6.47</min> 
<stddev>0.46</stddev> 
<median>6.53</median> 
<percentile>6.49</percentile> 
</sell> 
<all> 
<volume>58848103634</volume> 
<avg>6.15</avg> 
<max>8.85</max> 
<min>2.96</min> 
<stddev>0.75</stddev> 
<median>6.34</median> 
<percentile>4.60</percentile> 
</all> 
</type> 
<type id="35"> 
<buy> 
<volume>3893959033</volume> 
<avg>11.40</avg> 
<max>11.90</max> 
<min>7.05</min> 
<stddev>1.01</stddev> 
<median>11.71</median> 
<percentile>11.89</percentile> 
</buy> 
<sell> 
<volume>9116786204</volume> 
<avg>13.45</avg> 
<max>17.41</max> 
<min>11.98</min> 
<stddev>0.96</stddev> 
<median>12.30</median> 
<percentile>12.01</percentile> 
</sell> 
<all> 
<volume>13010745237</volume> 
<avg>12.83</avg> 
<max>17.41</max> 
<min>7.05</min> 
<stddev>1.10</stddev> 
<median>12.09</median> 
<percentile>10.42</percentile> 
</all> 
</type> 
<type id="36"> 
<buy> 
<volume>1287271345</volume> 
<avg>44.36</avg> 
<max>47.85</max> 
<min>23.10</min> 
<stddev>5.34</stddev> 
<median>47.06</median> 
<percentile>47.80</percentile> 
</buy> 
<sell> 
<volume>3753914760</volume> 
<avg>56.10</avg> 
<max>66.51</max> 
<min>48.87</min> 
<stddev>4.97</stddev> 
<median>54.00</median> 
<percentile>49.09</percentile> 
</sell> 
<all> 
<volume>5041186105</volume> 
<avg>53.10</avg> 
<max>66.51</max> 
<min>23.10</min> 
<stddev>5.86</stddev> 
<median>50.74</median> 
<percentile>37.13</percentile> 
</all> 
</type> 
<type id="37"> 
<buy> 
<volume>260386951</volume> 
<avg>122.15</avg> 
<max>130.02</max> 
<min>73.50</min> 
<stddev>14.82</stddev> 
<median>128.77</median> 
<percentile>130.01</percentile> 
</buy> 
<sell> 
<volume>721404372</volume> 
<avg>149.07</avg> 
<max>215.00</max> 
<min>132.00</min> 
<stddev>16.06</stddev> 
<median>141.37</median> 
<percentile>132.75</percentile> 
</sell> 
<all> 
<volume>981791323</volume> 
<avg>141.93</avg> 
<max>215.00</max> 
<min>73.50</min> 
<stddev>17.79</stddev> 
<median>140.00</median> 
<percentile>98.15</percentile> 
</all> 
</type> 
<type id="38"> 
<buy> 
<volume>67805138</volume> 
<avg>701.02</avg> 
<max>726.00</max> 
<min>436.11</min> 
<stddev>92.94</stddev> 
<median>715.30</median> 
<percentile>722.45</percentile> 
</buy> 
<sell> 
<volume>254352194</volume> 
<avg>792.02</avg> 
<max>1227.41</max> 
<min>728.95</min> 
<stddev>78.64</stddev> 
<median>757.27</median> 
<percentile>729.99</percentile> 
</sell> 
<all> 
<volume>322157332</volume> 
<avg>772.86</avg> 
<max>1227.41</max> 
<min>436.11</min> 
<stddev>86.94</stddev> 
<median>740.90</median> 
<percentile>644.00</percentile> 
</all> 
</type> 
<type id="39"> 
<buy> 
<volume>27385528</volume> 
<avg>594.54</avg> 
<max>654.94</max> 
<min>401.00</min> 
<stddev>77.95</stddev> 
<median>580.03</median> 
<percentile>654.91</percentile> 
</buy> 
<sell> 
<volume>308516889</volume> 
<avg>973.62</avg> 
<max>1599.92</max> 
<min>681.96</min> 
<stddev>181.14</stddev> 
<median>779.83</median> 
<percentile>682.79</percentile> 
</sell> 
<all> 
<volume>338902417</volume> 
<avg>934.45</avg> 
<max>1599.92</max> 
<min>7.30</min> 
<stddev>218.01</stddev> 
<median>760.00</median> 
<percentile>456.32</percentile> 
</all> 
</type> 
<type id="40"> 
<buy> 
<volume>14578614</volume> 
<avg>1090.66</avg> 
<max>1481.00</max> 
<min>500.00</min> 
<stddev>340.57</stddev> 
<median>1387.51</median> 
<percentile>1480.15</percentile> 
</buy> 
<sell> 
<volume>170054750</volume> 
<avg>2496.43</avg> 
<max>3899.91</max> 
<min>1498.99</min> 
<stddev>689.33</stddev> 
<median>2222.44</median> 
<percentile>1514.55</percentile> 
</sell> 
<all> 
<volume>194933364</volume> 
<avg>2259.76</avg> 
<max>3899.91</max> 
<min>5.00</min> 
<stddev>733.71</stddev> 
<median>1950.92</median> 
<percentile>6.94</percentile> 
</all> 
</type> 
</marketstat> 
</evec_api> 
+0

getAttributeValue(null,「id」)的兩個實例都失敗了,還是隻有當您到達END_TAG時調用它的那個? – bstar55

+0

都失敗了我托盤失敗的每一件事 – fantazituborg

回答

0

這是你的問題,我相信這是隻是一個複製粘貼錯誤:

... 
    if (tagname.equalsIgnoreCase(KEY_volume)) { 
     curStackSite.setTypeID(xpp.getAttributeValue(null,"id")); <-- REMOVE THIS 
     curStackSite.setVolume(curText); 
    } else if (tagname.equalsIgnoreCase(KEY_avg)) { 
    ... 

您在END_TAG上調用getAttributeValue(),並且該屬性在那裏不存在。因此,對於curStackSite ID,您的最終結果將爲NULL。

+0

nop sry :(仍然沒有閱讀id到curstack :(但你是answere給了我一個使用調試器的理想海灣,我發現它的價值被讀入到拳頭,但後來latere在更改爲null :(所以我認爲我只是需要找到一種方法來確保vaibal只讀取一次pr類型組 編輯 5分鐘後,我看到,當賣標籤來臨時,我正在創建一個新的curstack與刪除唯一的那個在給定的時間加載的ID :( – fantazituborg