ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hey all.
I am working on a game and it uses xml to store item/character/map information etc. In Windows I used Code Synthesis XSD or something to do it and it was real slow for my small xml files, so I don't really want to do the DOM thing this time...
These files hold information that is parsed and put into classes so I don't need the whole thing in memory, I just wanna be able to go:
xml.open("character.xml");
name = xml->character->name();
level = xml->character->level();
for(i = xmlNodes("items"); i != i.end(); i++) {
itemList.push(i->itemID());
}
etc, etc.
Can someone help me? I just need a nudge in the right direction. I looked into libxml2 but it looks way to complicated for my needs, how is SAX?
What will be the extent of the structural depth and complexity? There is a huge difference between parsing and being able to use what you've parsed. Technically you can parse XML using a few hundred lines, but making it into something useful is a different issue. Do you just plan to serialize a structure, then store a set of XML-formatted lists of member values, or will the data have to be structured in, e.g., a binary tree, with arbitrary depth and nesting?
I'm not familiar with any specific XML parsers, but I've written one on my own. From what it sounds like, it would be massive overkill for what you need. Parsing is the simple part, so if you don't mind working with strings and you have some time then you could write a parser yourself. If you need some sort of complex structure other than filling a finite depth of member data then it will depend on your specific application.
ta0kira
PS It actually sounds like XML is overkill itself. If the files don't need to be user-editable, you could just store a list of character1.name = "Death Master"-like lines. Those are remarkably easy to parse, especially if you don't have to take into account extra whitespace, etc. Writing parsers is a great skill to have, so a good time to start is now, while your requirement is simple. Later on you can expand it with new features if necessary.
PPS If you're going to go to the trouble of having functions like xml->character->name();, you might as well just parse directly from/into the class that contains name.
I wanted to work with xml because it is a standard file format and easily editable/understandable without any special tools. I have used plain text files with the variables in before, they are definitely easier.
I might just make my own parser that just handles the files I give it, therefore just putting it into construction/destruction of classes.
I liked xml also because I could easily change things around and previous code still worked as long as those tags were still in; with my text files I usually had to change a whole lot of code everytime I moved things around.
The library is meant to be 'light' and may be missing some features that you would like - for example checking that nested tags are matched correctly (just an example - I can't remember if nested tags are checked).
libxml2 is a Gnome thing, and yes it is complicated.
Distribution: Kubuntu 8.10 || openSUSE 11.1 || Arch Linux 2008.06
Posts: 55
Thanked: 0
Original Poster
Looks great. Thanks I'll give it a try. I'd hate to have to make my own parser/file type, this kinda thing is such a small part of creating a game... Once this is done I can get ready to recode from DirectX into SDL... :/
I not 100% sure whether you can use it from your C++ application - but there is an API for Java called Castor that you might be able to use for your requirement.
In Windows I used Code Synthesis XSD or something to do it and it was real slow for my small xml files, so I don't really want to do the DOM thing this time...
These files hold information that is parsed and put into classes so I don't need the whole thing in memory, I just wanna be able to go:
xml.open("character.xml");
name = xml->character->name();
level = xml->character->level();
for(i = xmlNodes("items"); i != i.end(); i++) {
itemList.push(i->itemID());
}
While you say you don't want to have DOM, your code fragment is based on the assumption that there is some sort if an in-memory representation of XML stored in the 'xml' variable that you then query to populate your classes. If your want a substantial speed-up then you will need to get rid of the intermediate in-memory representation and parse XML data directly into your classes. Normally this means you will need to use an even-driven API such as SAX or XmlReader.
BTW, the CodeSynthesis XSD tool that you mentioned above provides two XML Schema to C++ mappings C++/Tree (in-memory) and C++/Parser (event-driven). It sounds like C++/Parser may be exactly what you want since it is SAX-based (you can use either Expat or Xerces-C++ as the underlying XML parser), quite fast, and is a lot easier to use than raw SAX. In particular, it does all the state management and text to data conversion for you.
Here is an example API you will get for the character element in the XML file you showed:
That is, you implement the name(), level(), etc. functions and then the runtime will call them as it parses the XML with the corresponding data which you can use to populate your classes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.