XML

Started by
5 comments, last by laztrezort 12 years, 7 months ago
I've read a few things that state the use of XML files to either (dynamically?) generate classes or instances of classes. I wondering if anyone could provide an example or two of what this could be used for and how exactly it is done, or a link to somewhere that explains it well.

Also, what are the advantages of using XML instead of, say, a structured .txt file?
Advertisement
I don't know a lot about this specifically but the process I believe is referred to as serialization. Try looking into that.
If you are using C#, serializing/deserializing class instances to/from XML is almost trivial. Otherwise, you can try loitering around Codeproject, I seem to remember many relevant articles over there. XML is a structured text file, just in a standardized format - the big advantage to that being that you can find code & libraries to manipulate XML for whatever language you are using without having to roll your own.

Edit: I should clarify, since I'm not sure what you are asking - Serialization is the process of storing data, typically to a file on the hard drive, for example to save game state. I assume this is what you asking about?
Not quite with the game state thing. Not sure specifically what I'm wanting to do with it. The project I'm working on now is basically just a mass of code that I'm making into a game just for the sake of practice and learning, so when I think of something I've heard/read about I try to find a niche for it in this game.

The idea I had though, was creating my enemies in this way instead of explicitly creating a class for each type. I'm not sure if this is exactly what this does or not, but that's why I'm asking :)

Also, what are the advantages of using XML instead of, say, a structured .txt file?

One advantage is that you can add new elements to your xml nodes without braking existing code.
Another advantage is that xml is structured as a hierarchy, while a text file would typically be flat.
Libraries that extract data from xml files can take advantage of this structure.

Some languages like C#, Go and others has methods that can store and retrieve objects to/from xml files automatically by looking at the class definition, but I'm not sure that is what you are talking about here.



I've read a few things that state the use of XML files to either (dynamically?) generate classes or instances of classes. I wondering if anyone could provide an example or two of what this could be used for and how exactly it is done, or a link to somewhere that explains it well.


I'm a chronic newbie, so please don't take my advice uncritically.

I think you might want to look at dynamic memory. If you were making a game with tiles in a grid you could take a number out of an external file and use it in a loop to generate new instances of a class, and then add a reference of each address into a container. It means you wouldn't need to recompile just to add new columns and rows, and you don't need names as long as you can keep track of the object's address. You might need to delete them afterwards depending on the language and how you go about it.

Is this what you meant, or am I being horribly patronising? (If so, I don't mean it.)

Not quite with the game state thing. Not sure specifically what I'm wanting to do with it. The project I'm working on now is basically just a mass of code that I'm making into a game just for the sake of practice and learning, so when I think of something I've heard/read about I try to find a niche for it in this game.

The idea I had though, was creating my enemies in this way instead of explicitly creating a class for each type. I'm not sure if this is exactly what this does or not, but that's why I'm asking :)


Trying out new things is a good learning experience - but be careful not to over complicate things :wink:

I don't think XML is what you are looking for here. There are many ways of dealing with game entities - class inheritance, composition, component-model, etc. Why do you think you would need a separate class for each type of enemy? Enemies should be similar enough to share a single class (or possible a small inheritance tree).

Unless, of course, you are talking about loading the enemy classes up with data, not defining the classes themselves, in which case XML could be a consideration.

This topic is closed to new replies.

Advertisement