0
\$\begingroup\$

I'm planning to put all my text dialogue/events in an XML file and then use either SAX or DOM parser in Java to retrieve the correct text.

I'm intending for there to be A LOT of text in this game, so which parser would make more sense to use?

I know SAX has to go through the whole document and I need the text to be retrieved more or less immediately while a DOM parser needs to store the DOM in memory and, as I said, I'm going to have a lot of text in it, plus the rest of the game requires memory too.

I'm developing for PC. Which parser would generally make more sense to use?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Typically, if you just want to retrieve one specific datum in an XML file (such as, e.g., the text of one specific dialog) a SAX parser will give you faster access times because it does not have to instantiate as many objects as the DOM parser does when it builds a complete tree-representation of the XML document. You say that the SAX parser has to go through the whole document, which is true, but the DOM parser has to do that too, of course, or else it couldn't build the document.

That said, you need to know your use case. Do you want to keep all text dialogue/events in memory at all times, or do you just occasionally want to read from disk? The DOM parser takes more time to read in a long document, but once you have the DOM tree, you can probably query specific data points faster than reading the XML again and again with a SAX parser.

However, if you plan on keeping all text in memory at all times, you might want to consider a completely different data structure than the DOM tree. If you think, e.g., about a HashMap or something like this as a custom data structure, than a SAX parser could be used to easily and quickly populate your data structure when the game is initialized. Using your own data structure might give you advantages in terms of in-memory size as well as access time.

Finally, XML in general might not be the best solution if you're actually thinking of tons of data. It's not the most human-friendly data format for one, but also, there are simply better (read: specifically made for that) solutions to handling large data. For example, you could use a slim data base, such as e.g. sqlite instead. This way, you don't have to worry about parsing at all.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for helping. Am I right to think that access time would be the most pressing concern over memory usage? I imagine reading a huge XML file from top to bottom may result in delays. I haven't used databases before. Would a database be ok for the sorts of branching structures you would find in game dialogue where you have multiple routes? Is a database suitable for storing long pieces of text? \$\endgroup\$ Commented Sep 26, 2016 at 11:36
  • 1
    \$\begingroup\$ "Premature optimization is the root of all evil." - I think a sane advise would be to start with the solution that you find the easiest to implement and see if it already meets your requirements. If not, you can try to speed it up, or to use less memory, whatever the problem is. It's easy to fall into the trap of coming up with a very sophisticated solution that is (a) difficult to get right when implementing it, (b) hard to maintain, and (c) overkill for the problem at hand. \$\endgroup\$
    – Thomas
    Commented Sep 26, 2016 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .