Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Games, XML, DOM and SAX  (Read 5826 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Posted 2005-07-12 08:12:48 »

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?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2005-07-12 10:06:50 »

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

Cas Smiley

Offline kevglass

« JGO Spiffy Duke »


Medals: 195
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2005-07-12 10:24:28 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #3 - Posted 2005-07-12 11:36:23 »

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2005-07-12 13:56:33 »

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 Smiley

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #5 - Posted 2005-07-12 13:59:55 »

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

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 Smiley

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #6 - Posted 2005-07-12 14:24:37 »

 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.

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #7 - Posted 2005-07-12 15:13:28 »

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #8 - Posted 2005-07-12 15:22:18 »

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.

Offline kevglass

« JGO Spiffy Duke »


Medals: 195
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2005-07-12 15:23:18 »

Or you could use a database?

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #10 - Posted 2005-07-12 16:19:29 »

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

Or you could use a database?

Kev


---------------------------

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

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.


Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #11 - Posted 2005-07-12 16:27:39 »

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.

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #12 - Posted 2005-07-12 16:57:34 »

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.


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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #13 - Posted 2005-07-12 17:36:18 »

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 :-))

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #14 - Posted 2005-07-12 17:59:37 »

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #15 - Posted 2005-07-12 18:11:30 »

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.
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)?

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #16 - Posted 2005-07-13 02:31:41 »

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.
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)

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #17 - Posted 2005-07-13 06:22:41 »

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #18 - Posted 2005-07-13 08:57:08 »

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 Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #19 - Posted 2005-07-13 09:00:38 »

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

Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #20 - Posted 2005-07-13 10:05:37 »

<map
   width="512"
   height="512"
   name="level1.map"
>
<floor>#CDATA[.....]</floor>
<objects><item type="sword" x="123" y="33" hidden="true"/></objects>
</map>

Cas Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #21 - Posted 2005-07-13 11:31:12 »

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).

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #22 - Posted 2005-07-13 12:24:59 »

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 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).

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #23 - Posted 2005-07-13 12:52:49 »

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 Smiley
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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline kevglass

« JGO Spiffy Duke »


Medals: 195
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #24 - Posted 2005-07-13 13:12:08 »

[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

Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #25 - Posted 2005-07-13 13:42:52 »

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.

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #26 - Posted 2005-07-13 17:11:32 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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:
1  
2  
XMLSer xml = new XMLSer(".\\test.xml");
xml.saveToFile(new Integer(3));


XML output:
1  
2  
3  
<?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?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #27 - Posted 2005-07-13 17:16:54 »

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.

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #28 - Posted 2005-07-13 17:21:30 »

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline drslinky1500

Senior Newbie





« Reply #29 - Posted 2005-07-13 18:41:43 »

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.
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!