2

我有XML从 Web 服务下载的文件,我遇到的问题是我无法让它在循环之外正确显示数据。

XML格式设置如下:

<section>
    <outage>
        <dummydata>test</dummydata>
   </outage>
    <outage>
        <dummydata>test2</dummydata>
   </outage>
</section>
<section>
   <outage>
    <dummydata>test3</dummydata>
   <outage>
</section>

请注意,某些部分可能会发生不止一次中断。正如您在下面看到的,计数是正确的,但一次中断来自 4。

01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 4
01-24 22:54:49.186: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.186: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.196: I/System.out(1924): Outages : 763674

这是完成所有工作的代码

for (int j = 0; j < arrPolygons.size(); j++) {

     if (isPointInPolygon(point, arrPolygons.get(j).getPoints())) {
      for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {
            outageData = new LinkedList<HashMap<String, String>>();
             Node ouageInfoNode = nodeList.item(outageInfo);

              Element ouageInfoElement = (Element) ouageInfoNode;
              NodeList ouageInfoList = ouageInfoElement.getElementsByTagName("Outage");
              System.out.println("Outage Count per sect "+ouageInfoList.getLength());
                 for (int outages = 0; outages < ouageInfoList.getLength(); outages++) {
                     Node outageNode = ouageInfoList.item(outages);
                     Element outageDataElement = (Element) outageNode;
                     NodeList outageDataList =outageDataElement.getElementsByTagName("outagenum");   

                     outageDataElement = (Element) outageDataList.item(0);
                     outageDataList = outageDataElement.getChildNodes();

                     outagenum = ((Node) outageDataList.item(0))
                            .getNodeValue();

                    //This is what I want to loop 
                      HashMap<String, String> map = new HashMap<String, String>();
                      map.put("outagenum", outagenum);
                    outageData.add(outages, map);                        
                    } 

                }
                  Toast.makeText(MainActivity.this, "Inside " + arrPolygons.get(j).getId()+" "+ outageData.get(j), Toast.LENGTH_LONG).show();

                  System.out.println("Outages : " + outageData.get(0));

              }
           }

         }
    }

我的结果应该看起来像

Index 0 - {test,test2}
Index 1 - {test3}

谁能告诉我我在这个循环中做错了什么?

4

1 回答 1

0
 for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {
            outageData = new LinkedList<HashMap<String, String>>();

应该

         outageData = new LinkedList<HashMap<String, String>>();
 for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {
于 2013-01-25T05:30:14.087 回答