Games, XML, DOM and SAX

I’m used to writing my own text file parser, but now it is becoming quite a chore when it comes to more complex things.
Normally I would have an entire map as a text file and parse each object one at a time.

I’m doing a bit of reading on this, I’m making my own parser/serialiser for DOM.
What do you guys recommend for going down this route?

Anyone with any experience with this stuff?

Don’t write your own, just use the built in one.

Cas :slight_smile:

Depends on the map a bit, but don’t assume XML is the correct format. It depresses me to watch people pusing binary map formats into XML because “its more readable”.

Kev

I’m trying to use XML as a portable database and as a way to easily test/modify data outside my code.
Unfortunately I couldn’t find anything open source or built into Java for that.

XML DOM and SAX are built in to Java.
Furthermore if you use the “Bean paradigm” with your classes you’ll also find that getting data into and out of XML can be made a little easier.

Cas :slight_smile:

Make my classes as Beans?
I don’t know much about JBeans.

I’ve used DOM4J (dom4j.org) for years to load config stuff and it’s pretty easy to use. Just create a SAXReader to read the whole document and use XPath paths to retrieve collections of data or specific data. From the Node interface you can request anything to select nodes and so forth. But first of all, do your really need XML data? My advice is if you need only a flat structure of data then simply use properties files.

It’s not flat data, it’s not only hierarchical but it’s of immense proportions. That’s why I’m using XML and trying to make it databas’ish.
DOM seems weird to use.

So you recommend SAX?
Thanks.

DOM is definitely not the way to go because the whole file will be loaded in memory in a DOM structure which takes tons of memory. If you really want to use XML, use SAX.

Personally, I’d go for flat files (CSV or something like that). It’s easy to parse, allows for hierarchical structures and has much less overhead than XML.

Or you could use a database?

Kev

Not very portable.
What happens if I want to run my game on a system that doesn’t have a database setup?


Never heard of these things. Does it have native Java support?

Is performance/memory more important than code maintenance and ease of use? What are your requirements? Using a SAX parser might be more performing and likely consumes less memory but on the other hand, think about code reuse. SAX is not the way to go for code maintenance since you have to intercept every XML tag. XPath provides you a powerful and simple mechanism to retrieve data.

Code maintenance and data reusability is very important. Access to the data should be short and simple (JDBC does offer this).
I’d prefer not to use XML due to it being big and difficult to maintain but it’s better than using my own text file and parser in the long run. I would much rather use a database as Kev suggested but I need the database to be portable.

Using XML files with a JDBC interface would be wonderful if possible.

Well, you could take a look at this: http://hsqldb.org/
You can just embed it in your program.

EDIT: maybe this is an option too: https://xlsql.dev.java.net/
although I have absolutely no experience with this thing (it just popped up in a google search :-))

The first link will be useless when JSR 225 is implemented.
The second link looks good but I’m still trying to find out if MySQL database as a file can be used.

XML might be big but difficult to maintain? It depends on the design of your document I guess. Using a database seems overkill if running on the client side. How do you handle database install, size, config, etc.? It’s like saying hey!, I need a highway to go to the store next door. Man, do you need to handle thousands of records or just multiple documents (< 100)?

Good point.

[quote]The first link will be useless when JSR 225 is implemented.
[/quote]
The 1st link is a database. JSR225 is about a standard API for XQuery engines. Not only are they 2 entirely different things, but I’d also advice to use technologies that are available here and now. If you want to use XQuery now, Saxon has support for XQuery (http://www.saxonica.com/documentation/using-xquery/embedding.html)
But since you mentioned that your map files will be huge, I still doubt if XML is the way to go. You’ll make something that’s already huge much much more huge.
If the data will be as huge as you say, I’d go for a format which is easily parsable for a computer (which XML is, compared to simple flat files or some binary format, not). If you need to be able to alter it by hand, it’s easy to do with a flat file and you will want to make an editor for the maps anyway, especially if the maps will become huge.

What kind of maps are we talking about anyway? (just to get some perspective)

So I should continue using flat files.
Thanks.

Maps will store every little piece of information it can.
Night, day, weather, entities, and other states.

Y’all seem to have forgotten that XML can store encoded binary data… so XML might still be a perfectly fine format for maps, as you can put all the complex meta-map information in to XML very nicely. I was planning on doing it this way myself.

Cas :slight_smile:

Can you give an example of what you would want to store in XML rather than the (encoded) binary data?