【问题标题】:Android XML pull parser cannot get attributesAndroid XML 拉解析器无法获取属性
【发布时间】:2014-06-11 18:09:10
【问题描述】:

我正在尝试将 this xml file 加载到 Android 应用程序中。我让一切正常,但由于某种原因我无法加载id。 我一直在谷歌搜索,发现我应该使用parser.getattribute(null,"id"); 但由于某种原因,它不断返回null。我也尝试过parser.getattribute(0);parser.getattribute(3);,这给了我一个越界异常,但是当我通过程序调试并查看我的解析器类时,值3 总是包含id

这确实是一个简单的修复,但是有人知道我如何访问每个数据集的 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>

【问题讨论】:

  • getAttributeValue(null, "id") 的两个实例是否都失败了,或者只是您在到达 END_TAG 时调用它的那个实例?
  • 两者都失败了我托盘失败的每一件事

标签: android xml parsing pull


【解决方案1】:

这是你的问题,我认为这只是一个复制粘贴错误:

    ...
    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。

【讨论】:

  • nop sry :( 仍然没有将 id 读入 curstack :( 但你的回答给了我一个使用调试器的理想空间更改为 null :( 所以我想我只需要找到一种方法来确保仅读取一次 pr 类型组编辑 5 分钟后,我看到当出售标签开始时,我正在创建一个新的 curstack,并删除了唯一的在给定时间加载的 id :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 2012-09-27
相关资源
最近更新 更多