Games, XML, DOM and SAX

<map
width=“512”
height=“512”
name=“level1.map”

#CDATA[…]

Cas :slight_smile:

I see.

I guess it could be a good way to use XML while developing the maps (and thus benefit from XML’s flexibilty and readability) and finally serializing the maps in a lean and mean binary format when you ship if needed (when you want to target machines with tight memory constraints, when the XML becomes very huge and loading the maps becomes slow, etc).

Then how are you supposed to seperate data from your model?

I’m currently using this format:

MAP

Sprite 50, 100, c:\pics\tile.png
Sprite 100, 25, c:\pics\tile.png

I’ve written my own text parser with the help of RegEx to pass files like this.

I generally find it easier to have a tag in the XML which includes a path or filename to a referenced binary resource rather than binary data itself. Packing binary chunks into a text file just feels icky.

[I love me too posts but…]

Me too! Apart from anything else, packing the binary into the XML makes many text editors barf when I want to quickly hack at the XML.

Kev

And if I remember correctly, binary XML is base64 encoded so it’s not the best method to save space. I agree with Orangy Tang that referencing binary files from XML should be better. On the other hand, you have to open more files, which shouldn’t be a significant overhead.

Guys, I figured that I want to be a lazy bastard.
So guess what I’ve done?

I’ve saved myself a lot of work.

My new serializer:


public class XMLSer {
	
	private XMLDecoder dec;
	private XMLEncoder enc;
	
	
	public XMLSer(final String filePath) {
		try {
			dec = new XMLDecoder(new FileInputStream(new File(filePath)));
			enc = new XMLEncoder(new FileOutputStream(new File(filePath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public <E>E loadFromFile() {
		return (E)dec.readObject();
	}
	
	public <E>void saveToFile(E obj) {
		enc.writeObject(obj);
		enc.flush();
	}

}

Work required to store an object:


XMLSer xml = new XMLSer(".\\test.xml");
xml.saveToFile(new Integer(3));

XML output:


<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.5.0_04" class="java.beans.XMLDecoder"> 
 <int>3</int> 

What do you guys think about Sun’s nice XML serializer?

Are you aware of the class version problems? How do you handle that? Once you change your class and you try to load your file, your risk to have compatibility problems.

I’ll fix that up later.
Now I’m simply after something that will do most of the work for me easily.

If you are wanting an embeded database, derby(cloudscape) is a nice Java database. You can also get the source code for this database and plug it directly into your program.

It is currently held at the apache incubator website. Apache also has an XML parser for java, but I have not personally used it.

Yeah, that’s what I’d do these days, and then do something better later :).