2012-02-21 41 views
0

我有這個工作,做了一個改變的地方,現在我無法追查到錯誤。android parse xml - Nodelist有太多的對象

下面是情況...當_regionList創建時,xml文件中有13個區域用於我正在使用的區域,但_regionlist/children /數組有18個對象。使用不同的區域(50個元素),它創建60個對象。

當然,數組中的其餘對象有一個空ID,這是問題所在。有關額外物體如何投入的想法?

感謝

//解析XML文件

public void Parse(InputStream inStream, String inRegion) 
{ 
    try { 
     String _region = inRegion; 

     this.cFactory = DocumentBuilderFactory.newInstance(); 
     this.cBuilder = this.cFactory.newDocumentBuilder(); 
     this.cBuilder.isValidating(); 

     Document _document = this.cBuilder.parse(inStream, null); 

     _document.getDocumentElement().normalize(); 

     NodeList _regionList = _document.getElementsByTagName(_region); 
     final int _length = _regionList.getLength(); 

     for (int i = 0; i < _length; i++) { 
      final NamedNodeMap _attr = _regionList.item(i).getAttributes(); 
      final String _regionName = GetNodeValue(_attr, "name"); 

      // Construct a Region object 
      PropertiesRegion _regionObject = new PropertiesRegion(_regionName); 

      // Add to list 
      this.cList.add(_regionObject); 

      Log.d(TAG, _regionObject.toString()); 
     } 
    } 

    catch (SAXException e) 
    { 
     e.printStackTrace(); 
    } 

    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    catch (ParserConfigurationException e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

_document,_regionList和cList全部用額外的數組元素初始化。 TrimToSize似乎是合適的解決方案,但這不是一個有效的選擇。不知道我有什麼其他選項去除多餘的數組索引。想法? – user1222760 2012-02-22 07:07:16

回答

0

ArrayList(集合)中的其他空索引是好的。問題出在TextView中,並將其連接到ArrayList。

0

NodeList中的其他元素是最有可能
,等等,等等,然後再繼續檢查節點類型,可以幫助您解決您的問題:

Node node=_regionList.item(i); 
if(node.getNodeType()!=3) 
{ 
     final NamedNodeMap _attr = node.getAttributes(); 
     final String _regionName = GetNodeValue(_attr, "name"); 

     // Construct a Region object 
     PropertiesRegion _regionObject = new PropertiesRegion(_regionName); 

     // Add to list 
     this.cList.add(_regionObject); 

     Log.d(TAG, _regionObject.toString()); 

}