What I'm doing is making a game and I need to do this for my items:

  • Have an "Item" Class
  • Have other superclasses such as:Weapon,Armor,Food
  • Read XML file with items on it.
  • Create appropriate Item classes with the correct extended class.
  • Fill the fields with data from the XML class

I have no idea how to do this, can you guys help me see what I need to do?

Thanks.

java has xml parser

Where are you stuck - how far have you got?

I've created the XML file and I've just been thinking about how to do the rest. It doesn't really make sense to me though.

Just take it one step at a time.

First you need to create your Item class and its subclasses - start with just one subclass, you can add the others when you have got one working.
Then you need to read the XML file
Then you get the first item's type from the XML info
Then you create an object of the right type
Then you get the item's data from the XML info
Then you set the object's fields using that data.
Then you repeat all that until you have used all the data.

Don't even think of going on to the next step until you have coded, tested and de-bugged the previous step. Use lots of temporary print statements to confirm that each step is working before trying to do anything else.

If/when you get stuck, post your problem and your progress so far here for help

Okay, I went and did a little more research on the topic and I downloaded the Jdom library for handling XML in Java. it seems to be very easy to use, I got all the steps done, but when I go to use the item, I get a nullPointerException. I'll post my code, maybe you know what's going on.

    public void populateItems() throws SlickException {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document itemDoc = builder.build("res/ItemFile.xml");
            Element rootNode = itemDoc.getRootElement();
            List list = rootNode.getChildren("item");

            for (int i = 0; i < list.size(); i++) {
                Element node = (Element) list.get(i);

                if(node.getChildText("type").equals("weapon")){
                    item = new Weapon();
                    item.name = node.getChildText("name");
                    ((Weapon) item).setDamage(Integer.parseInt(node.getChildText("dmg")));
                    items.add(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Forgot to mention, I get the error on the
items.add(item);
statement.

Looksd like items isn't initialised?

Yeah, I realized that about 20 seconds after I posted it. Thanks

When that happens please edit or update your post so that other people don't waste their time trying to help you solve a problem that you have already solved. In this case no big deal, but sometimes people do a lot of work to help identify a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.