2011-09-18 61 views
2

我遇到了這個XML解析器中的崩潰我寫了在將節點轉換爲元素時,Dom XML解析器異常

public class LevelParser { 
    Level parsedData=new Level(); 
    public Level getParsedData() { 
     return parsedData; 
    } 
    public void parseXml(InputStream parseFile, int wantedLevel){ 
     Document doc; 
     try { 
      doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parseFile); 
      Element root=doc.getDocumentElement(); 

      NodeList levels = root.getElementsByTagName("level"); 

      /* for levels*/ 
      for(int i=0;i<levels.getLength();i++) { 
       Node c= levels.item(i); 

       Element note=(Element)c; 
       Debug.i("parser arrived here"); 

       int level = Integer.parseInt(note.getAttribute("id")); 

       //only load the wanted level; 
       if(level != wantedLevel) 
        continue; 

       parsedData.setLevel(level); 
       parsedData.setBackgroundName(note.getAttribute("backgroundname")); 

       NodeList noteDetails=c.getChildNodes(); 

       for(int j=0;j<noteDetails.getLength();j++) { 
        Node c1=noteDetails.item(j); 
        if(c1.getNodeType()==Node.ELEMENT_NODE) { 
          Debug.i("parser arrived here1"); 
          Element detail=(Element)c1; 
          Debug.i("parser arrived here2"); 
          String nodeName=detail.getNodeName(); 

          if(nodeName.equals("EnemyGroup")) { 
           Vector<Integer> temp_locations = parsedData.getEnemyLocations(); 
           NodeList enemygroup=c1.getChildNodes(); 

           parsedData.setEnemyNumber(enemygroup.getLength()); 

           for(int x = 0;x < enemygroup.getLength(); x++) { 
            Debug.i("parser arrived here3.0"); 
            Element location=(Element)enemygroup.item(x); 
            Debug.i("parser arrived here4.0"); 
            temp_locations.add(new Integer(Integer.parseInt(location.getFirstChild().getNodeValue()))); 
           } 
           parsedData.setEnemyLocations(temp_locations); 
          } 
          if(nodeName.equals("PlayerGroup")) { 
          Vector<Integer> temp_locations = parsedData.getPlayerLocations(); 
          NodeList playergroup=c1.getChildNodes(); 
          parsedData.setPlayerNumber(playergroup.getLength()); 
          for(int x=0;x<playergroup.getLength();x++) { 
            Debug.i("parser arrived here3.1"); 
            Element location=(Element)playergroup.item(x); 
            Debug.i("parser arrived here4.1"); 
            temp_locations.add(new Integer(Integer.parseInt(location.getFirstChild().getNodeValue()))); 
          } 
          parsedData.setPlayerLocations(temp_locations); 
          } 
          if(nodeName.equals("MonsterGroupLocations")) { 
           Vector<Location> temp_locations = parsedData.getEmptyLocations(); 
           NodeList emptygroup=c1.getChildNodes(); 
           parsedData.setEmptyNumber(emptygroup.getLength()); 
           for(int x=0;x<emptygroup.getLength();x++) { 
            Debug.i("parser arrived here3.2"); 
            Element location=(Element)emptygroup.item(x); 
            Debug.i("parser arrived here4.2"); 
            int xl = Integer.parseInt(location.getAttribute("x")); 
            int yl = Integer.parseInt(location.getAttribute("y")); 
            temp_locations.add(new Location(xl , yl)); 
           } 
           parsedData.setEmptyLocations(temp_locations); 
          } 
         } 
        } 
      } 
     } catch (SAXException e) { 
      Debug.e(e.toString()); 
     } catch (IOException e) { 
      Debug.e(e.toString()); 
     } catch (ParserConfigurationException e) { 
      Debug.e(e.toString()); 
     } catch (FactoryConfigurationError e) { 
      Debug.e(e.toString()); 
     } 
    } 
} 

它在第112行崩潰了ClassCastException。我真的不明白爲什麼,因爲我使用相同的代碼前幾行沒有任何崩潰。

我正在解析這個XML

<?xml version="1.0" encoding="utf-8"?> 
<levelinfos> 
    <level id="1" backgroundname="back_ground_level_1.png"> 
     <MonsterGroupLocations> 
      <location x="100" y="150"></location> 
      <location x="250" y="200"></location> 
     </MonsterGroupLocations> 
     <EnemyGroup> 
      <index>0</index> 
     </EnemyGroup> 
     <PlayerGroup> 
      <index>1</index> 
     </PlayerGroup> 
    </level> 
</levelinfos> 

在調試時我看到它將Level.enemyNumber設置爲5,而它應該只是2 ..所以它可能還有其他錯誤。我在XML中沒有經驗,也許我在做一個基本的錯誤..

回答

2

您正在嘗試將文本節點投射到元素。請注意,Node#childNodes()返回節點的所有直接子節點,包括Element,Text,CData,Comment和ProcessingInstruction節點,而不僅僅是元素。您需要在使用它時過濾NodeList,或者使用僅返回元素的便利方法。我通常有這樣的事情:

public static List<Element> elements(Node parent) { 
    List<Element> result = new LinkedList<Element>(); 
    NodeList nl = parent.getChildNodes(); 
    for (int i = 0; i < nl.getLength(); ++i) { 
     if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) 
      result.add((Element) nl.item(i)); 
    } 
    return result; 
} 

您可以直接用這個循環:

for (Element location: elements(c1) { 
    ... 
} 
+0

工程就像一個魅力,謝謝! –