Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:49:54+00:00 2026-05-25T16:49:54+00:00

I’m experiencing a crash in this XML parser I wrote public class LevelParser {

  • 0

I’m experiencing a crash in this XML parser I wrote

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());
        }
    }
}

It crashes at line 112 with a ClassCastException. I don’t really understand why, as I use the same code few lines before without any crash.

I’m parsing this 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>

While debugging I saw that it sets the Level.enemyNumber to 5, while it should be only 2.. so it’s possible there are other errors. I’m not really experienced in XML, maybe I’m doing a basic mistake..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:49:55+00:00Added an answer on May 25, 2026 at 4:49 pm

    You are trying to cast a Text node to an Element. Note that Node#childNodes() returns all direct children of the node, including Element, Text, CData, Comment and ProcessingInstruction nodes, not just elements. You need to either filter the NodeList when using it or use a convenience method that returns just elements. I usually have something like this:

    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;
    }
    

    You can use this directly in a for loop:

    for (Element location: elements(c1) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm looking for suggestions for debugging... If you view this site in Firefox or
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.